wsgi-standalone.rst
This file
Path
docs/deploying/wsgi-standalone.rst
132 lines · 4.2 KiB
Last changed by
Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb
History of this fileTake it
Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb
lines 1–132why, one step back1.. _deploying-wsgi-standalone:23Standalone WSGI Containers4==========================56There are popular servers written in Python that contain WSGI applications and7serve HTTP. These servers stand alone when they run; you can proxy to them8from your web server. Note the section on :ref:`deploying-proxy-setups` if you9run into issues.1011Gunicorn12--------1314`Gunicorn`_ 'Green Unicorn' is a WSGI HTTP Server for UNIX. It's a pre-fork15worker model ported from Ruby's Unicorn project. It supports both `eventlet`_16and `greenlet`_. Running a Flask application on this server is quite simple::1718 gunicorn myproject:app1920`Gunicorn`_ provides many command-line options -- see ``gunicorn -h``.21For example, to run a Flask application with 4 worker processes (``-w224``) binding to localhost port 4000 (``-b 127.0.0.1:4000``)::2324 gunicorn -w 4 -b 127.0.0.1:4000 myproject:app2526.. _Gunicorn: http://gunicorn.org/27.. _eventlet: http://eventlet.net/28.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html2930Tornado31--------3233`Tornado`_ is an open source version of the scalable, non-blocking web34server and tools that power `FriendFeed`_. Because it is non-blocking and35uses epoll, it can handle thousands of simultaneous standing connections,36which means it is ideal for real-time web services. Integrating this37service with Flask is straightforward::3839 from tornado.wsgi import WSGIContainer40 from tornado.httpserver import HTTPServer41 from tornado.ioloop import IOLoop42 from yourapplication import app4344 http_server = HTTPServer(WSGIContainer(app))45 http_server.listen(5000)46 IOLoop.instance().start()474849.. _Tornado: http://www.tornadoweb.org/50.. _FriendFeed: http://friendfeed.com/5152Gevent53-------5455`Gevent`_ is a coroutine-based Python networking library that uses56`greenlet`_ to provide a high-level synchronous API on top of `libevent`_57event loop::5859 from gevent.wsgi import WSGIServer60 from yourapplication import app6162 http_server = WSGIServer(('', 5000), app)63 http_server.serve_forever()6465.. _Gevent: http://www.gevent.org/66.. _greenlet: http://codespeak.net/py/0.9.2/greenlet.html67.. _libevent: http://monkey.org/~provos/libevent/6869.. _deploying-proxy-setups:7071Proxy Setups72------------7374If you deploy your application using one of these servers behind an HTTP proxy75you will need to rewrite a few headers in order for the application to work.76The two problematic values in the WSGI environment usually are `REMOTE_ADDR`77and `HTTP_HOST`. You can configure your httpd to pass these headers, or you78can fix them in middleware. Werkzeug ships a fixer that will solve some common79setups, but you might want to write your own WSGI middleware for specific80setups.8182Here's a simple nginx configuration which proxies to an application served on83localhost at port 8000, setting appropriate headers:8485.. sourcecode:: nginx8687 server {88 listen 80;8990 server_name _;9192 access_log /var/log/nginx/access.log;93 error_log /var/log/nginx/error.log;9495 location / {96 proxy_pass http://127.0.0.1:8000/;97 proxy_redirect off;9899 proxy_set_header Host $host;100 proxy_set_header X-Real-IP $remote_addr;101 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;102 }103 }104105If your httpd is not providing these headers, the most common setup invokes the106host being set from `X-Forwarded-Host` and the remote address from107`X-Forwarded-For`::108109 from werkzeug.contrib.fixers import ProxyFix110 app.wsgi_app = ProxyFix(app.wsgi_app)111112.. admonition:: Trusting Headers113114 Please keep in mind that it is a security issue to use such a middleware in115 a non-proxy setup because it will blindly trust the incoming headers which116 might be forged by malicious clients.117118If you want to rewrite the headers from another header, you might want to119use a fixer like this::120121 class CustomProxyFix(object):122123 def __init__(self, app):124 self.app = app125126 def __call__(self, environ, start_response):127 host = environ.get('HTTP_X_FHOST', '')128 if host:129 environ['HTTP_HOST'] = host130 return self.app(environ, start_response)131132 app.wsgi_app = CustomProxyFix(app.wsgi_app)