Skip to content
Myr

wsgi-standalone.rst

This file

Path

docs/deploying/wsgi-standalone.rst

155 lines · 5.1 KiB

Last changed by

Merge branch '1.0.x'David Lordbefore adoption2236ba9

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:app2526The ``gunicorn`` command expects the names of your application module or27package and the application instance within the module. If you use the28application factory pattern, you can pass a call to that::2930    $ gunicorn "myproject:create_app()"3132.. _Gunicorn: https://gunicorn.org/33.. _eventlet: https://eventlet.net/343536uWSGI37--------3839`uWSGI`_ is a fast application server written in C. It is very configurable40which makes it more complicated to setup than gunicorn.4142Running `uWSGI HTTP Router`_::4344    $ uwsgi --http 127.0.0.1:5000 --module myproject:app4546For a more optimized setup, see :doc:`configuring uWSGI and NGINX <uwsgi>`.4748.. _uWSGI: https://uwsgi-docs.readthedocs.io/en/latest/49.. _uWSGI HTTP Router: https://uwsgi-docs.readthedocs.io/en/latest/HTTP.html#the-uwsgi-http-https-router5051Gevent52-------5354`Gevent`_ is a coroutine-based Python networking library that uses55`greenlet`_ to provide a high-level synchronous API on top of `libev`_56event loop::5758    from gevent.pywsgi import WSGIServer59    from yourapplication import app6061    http_server = WSGIServer(('', 5000), app)62    http_server.serve_forever()6364.. _Gevent: http://www.gevent.org/65.. _greenlet: https://greenlet.readthedocs.io/en/latest/66.. _libev: http://software.schmorp.de/pkg/libev.html6768Twisted Web69-----------7071`Twisted Web`_ is the web server shipped with `Twisted`_, a mature,72non-blocking event-driven networking library. Twisted Web comes with a73standard WSGI container which can be controlled from the command line using74the ``twistd`` utility::7576    $ twistd web --wsgi myproject.app7778This example will run a Flask application called ``app`` from a module named79``myproject``.8081Twisted Web supports many flags and options, and the ``twistd`` utility does82as well; see ``twistd -h`` and ``twistd web -h`` for more information. For83example, to run a Twisted Web server in the foreground, on port 8080, with an84application from ``myproject``::8586    $ twistd -n web --port tcp:8080 --wsgi myproject.app8788.. _Twisted: https://twistedmatrix.com/89.. _Twisted Web: https://twistedmatrix.com/trac/wiki/TwistedWeb9091.. _deploying-proxy-setups:9293Proxy Setups94------------9596If you deploy your application using one of these servers behind an HTTP proxy97you will need to rewrite a few headers in order for the application to work.98The two problematic values in the WSGI environment usually are ``REMOTE_ADDR``99and ``HTTP_HOST``.  You can configure your httpd to pass these headers, or you100can fix them in middleware.  Werkzeug ships a fixer that will solve some common101setups, but you might want to write your own WSGI middleware for specific102setups.103104Here's a simple nginx configuration which proxies to an application served on105localhost at port 8000, setting appropriate headers:106107.. sourcecode:: nginx108109    server {110        listen 80;111112        server_name _;113114        access_log  /var/log/nginx/access.log;115        error_log  /var/log/nginx/error.log;116117        location / {118            proxy_pass         http://127.0.0.1:8000/;119            proxy_redirect     off;120121            proxy_set_header   Host                 $host;122            proxy_set_header   X-Real-IP            $remote_addr;123            proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;124            proxy_set_header   X-Forwarded-Proto    $scheme;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.middleware.proxy_fix import ProxyFix133    app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)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)