others.rst
This file
Path
docs/deploying/others.rst
102 lines · 3.3 KiB
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than de3b6ab point at it.
Fixed a broken sentence about blueprints.Armin Ronacherbefore adoptionde3b6ab
lines 1–102why, one step back1.. _deploying-other-servers:23Other Servers4=============56There are popular servers written in Python that allow the execution of WSGI7applications as well. These servers stand alone when they run; you can proxy8to them from your web server.910Tornado11--------1213`Tornado`_ is an open source version of the scalable, non-blocking web14server and tools that power `FriendFeed`_. Because it is non-blocking and15uses epoll, it can handle thousands of simultaneous standing connections,16which means it is ideal for real-time web services. Integrating this17service with Flask is a trivial task::1819 from tornado.wsgi import WSGIContainer20 from tornado.httpserver import HTTPServer21 from tornado.ioloop import IOLoop22 from yourapplication import app2324 http_server = HTTPServer(WSGIContainer(app))25 http_server.listen(5000)26 IOLoop.instance().start()272829.. _Tornado: http://www.tornadoweb.org/30.. _FriendFeed: http://friendfeed.com/3132Gevent33-------3435`Gevent`_ is a coroutine-based Python networking library that uses36`greenlet`_ to provide a high-level synchronous API on top of `libevent`_37event loop::3839 from gevent.wsgi import WSGIServer40 from yourapplication import app4142 http_server = WSGIServer(('', 5000), app)43 http_server.serve_forever()4445.. _Gevent: http://www.gevent.org/46.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html47.. _libevent: http://monkey.org/~provos/libevent/4849Gunicorn50--------5152`Gunicorn`_ 'Green Unicorn' is a WSGI HTTP Server for UNIX. It's a pre-fork53worker model ported from Ruby's Unicorn project. It supports both `eventlet`_54and `greenlet`_. Running a Flask application on this server is quite simple::5556 gunicorn myproject:app5758`Gunicorn`_ provides many command-line options -- see ``gunicorn -h``.59For example, to run a Flask application with 4 worker processes (``-w604``) binding to localhost port 4000 (``-b 127.0.0.1:4000``)::6162 gunicorn -w 4 -b 127.0.0.1:4000 myproject:app6364.. _Gunicorn: http://gunicorn.org/65.. _eventlet: http://eventlet.net/66.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html6768Proxy Setups69------------7071If you deploy your application using one of these servers behind an HTTP72proxy you will need to rewrite a few headers in order for the73application to work. The two problematic values in the WSGI environment74usually are `REMOTE_ADDR` and `HTTP_HOST`. Werkzeug ships a fixer that75will solve some common setups, but you might want to write your own WSGI76middleware for specific setups.7778The most common setup invokes the host being set from `X-Forwarded-Host`79and the remote address from `X-Forwarded-For`::8081 from werkzeug.contrib.fixers import ProxyFix82 app.wsgi_app = ProxyFix(app.wsgi_app)8384Please keep in mind that it is a security issue to use such a middleware85in a non-proxy setup because it will blindly trust the incoming86headers which might be forged by malicious clients.8788If you want to rewrite the headers from another header, you might want to89use a fixer like this::9091 class CustomProxyFix(object):9293 def __init__(self, app):94 self.app = app9596 def __call__(self, environ, start_response):97 host = environ.get('HTTP_X_FHOST', '')98 if host:99 environ['HTTP_HOST'] = host100 return self.app(environ, start_response)101102 app.wsgi_app = CustomProxyFix(app.wsgi_app)