Skip to content
Myr

wsgi-standalone.rst

This file

Path

docs/deploying/wsgi-standalone.rst

155 lines · 5.0 KiB

Last changed by

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoptionb0f8d0f

History of this file

Take it

1.. _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://greenlet.readthedocs.org/en/latest/2930Tornado31--------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://greenlet.readthedocs.org/en/latest/67.. _libevent: http://monkey.org/~provos/libevent/6869Twisted Web70-----------7172`Twisted Web`_ is the web server shipped with `Twisted`_, a mature,73non-blocking event-driven networking library. Twisted Web comes with a74standard WSGI container which can be controlled from the command line using75the ``twistd`` utility::7677    twistd web --wsgi myproject.app7879This example will run a Flask application called ``app`` from a module named80``myproject``.8182Twisted Web supports many flags and options, and the ``twistd`` utility does83as well; see ``twistd -h`` and ``twistd web -h`` for more information. For84example, to run a Twisted Web server in the foreground, on port 8080, with an85application from ``myproject``::8687    twistd -n web --port 8080 --wsgi myproject.app8889.. _Twisted: https://twistedmatrix.com/90.. _Twisted Web: https://twistedmatrix.com/trac/wiki/TwistedWeb9192.. _deploying-proxy-setups:9394Proxy Setups95------------9697If you deploy your application using one of these servers behind an HTTP proxy98you will need to rewrite a few headers in order for the application to work.99The two problematic values in the WSGI environment usually are `REMOTE_ADDR`100and `HTTP_HOST`.  You can configure your httpd to pass these headers, or you101can fix them in middleware.  Werkzeug ships a fixer that will solve some common102setups, but you might want to write your own WSGI middleware for specific103setups.104105Here's a simple nginx configuration which proxies to an application served on106localhost at port 8000, setting appropriate headers:107108.. sourcecode:: nginx109110    server {111        listen 80;112113        server_name _;114115        access_log  /var/log/nginx/access.log;116        error_log  /var/log/nginx/error.log;117118        location / {119            proxy_pass         http://127.0.0.1:8000/;120            proxy_redirect     off;121122            proxy_set_header   Host             $host;123            proxy_set_header   X-Real-IP        $remote_addr;124            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;125        }126    }127128If your httpd is not providing these headers, the most common setup invokes the129host being set from `X-Forwarded-Host` and the remote address from130`X-Forwarded-For`::131132    from werkzeug.contrib.fixers import ProxyFix133    app.wsgi_app = ProxyFix(app.wsgi_app)134135.. admonition:: Trusting Headers136137   Please keep in mind that it is a security issue to use such a middleware in138   a non-proxy setup because it will blindly trust the incoming headers which139   might be forged by malicious clients.140141If you want to rewrite the headers from another header, you might want to142use a fixer like this::143144    class CustomProxyFix(object):145146        def __init__(self, app):147            self.app = app148149        def __call__(self, environ, start_response):150            host = environ.get('HTTP_X_FHOST', '')151            if host:152                environ['HTTP_HOST'] = host153            return self.app(environ, start_response)154155    app.wsgi_app = CustomProxyFix(app.wsgi_app)