Skip to content
Myr

wsgi-standalone.rst

This file

Path

docs/deploying/wsgi-standalone.rst

134 lines · 4.4 KiB

Last changed by

Convert readthedocs link for their .org -> .io migration for hosted projects (#1827)Adam Chainzbefore adoption70de011

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: https://greenlet.readthedocs.io/en/latest/2930Gevent31-------3233`Gevent`_ is a coroutine-based Python networking library that uses34`greenlet`_ to provide a high-level synchronous API on top of `libev`_35event loop::3637    from gevent.wsgi import WSGIServer38    from yourapplication import app3940    http_server = WSGIServer(('', 5000), app)41    http_server.serve_forever()4243.. _Gevent: http://www.gevent.org/44.. _greenlet: https://greenlet.readthedocs.io/en/latest/45.. _libev: http://software.schmorp.de/pkg/libev.html4647Twisted Web48-----------4950`Twisted Web`_ is the web server shipped with `Twisted`_, a mature,51non-blocking event-driven networking library. Twisted Web comes with a52standard WSGI container which can be controlled from the command line using53the ``twistd`` utility::5455    twistd web --wsgi myproject.app5657This example will run a Flask application called ``app`` from a module named58``myproject``.5960Twisted Web supports many flags and options, and the ``twistd`` utility does61as well; see ``twistd -h`` and ``twistd web -h`` for more information. For62example, to run a Twisted Web server in the foreground, on port 8080, with an63application from ``myproject``::6465    twistd -n web --port 8080 --wsgi myproject.app6667.. _Twisted: https://twistedmatrix.com/68.. _Twisted Web: https://twistedmatrix.com/trac/wiki/TwistedWeb6970.. _deploying-proxy-setups:7172Proxy Setups73------------7475If you deploy your application using one of these servers behind an HTTP proxy76you will need to rewrite a few headers in order for the application to work.77The two problematic values in the WSGI environment usually are ``REMOTE_ADDR``78and ``HTTP_HOST``.  You can configure your httpd to pass these headers, or you79can fix them in middleware.  Werkzeug ships a fixer that will solve some common80setups, but you might want to write your own WSGI middleware for specific81setups.8283Here's a simple nginx configuration which proxies to an application served on84localhost at port 8000, setting appropriate headers:8586.. sourcecode:: nginx8788    server {89        listen 80;9091        server_name _;9293        access_log  /var/log/nginx/access.log;94        error_log  /var/log/nginx/error.log;9596        location / {97            proxy_pass         http://127.0.0.1:8000/;98            proxy_redirect     off;99100            proxy_set_header   Host                 $host;101            proxy_set_header   X-Real-IP            $remote_addr;102            proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;103            proxy_set_header   X-Forwarded-Proto    $scheme;104        }105    }106107If your httpd is not providing these headers, the most common setup invokes the108host being set from ``X-Forwarded-Host`` and the remote address from109``X-Forwarded-For``::110111    from werkzeug.contrib.fixers import ProxyFix112    app.wsgi_app = ProxyFix(app.wsgi_app)113114.. admonition:: Trusting Headers115116   Please keep in mind that it is a security issue to use such a middleware in117   a non-proxy setup because it will blindly trust the incoming headers which118   might be forged by malicious clients.119120If you want to rewrite the headers from another header, you might want to121use a fixer like this::122123    class CustomProxyFix(object):124125        def __init__(self, app):126            self.app = app127128        def __call__(self, environ, start_response):129            host = environ.get('HTTP_X_FHOST', '')130            if host:131                environ['HTTP_HOST'] = host132            return self.app(environ, start_response)133134    app.wsgi_app = CustomProxyFix(app.wsgi_app)