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

The walk stopped after 200 commits: lines older than c74f469 point at it.

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 1–17why, one step back
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::17

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 18why, one step back
18 $ gunicorn myproject:app

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 19–23why, one step back
19
20`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``)::23

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 24why, one step back
24 $ gunicorn -w 4 -b 127.0.0.1:4000 myproject:app

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 25why, one step back
25

Merge branch '1.0.x'David Lordbefore adoption2236ba9

lines 26–31why, one step back
26The ``gunicorn`` command expects the names of your application module or
27package 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()"31

Merge pull request #2969 from codeaditya/update-linksDavid Lordbefore adoption7e714bd

lines 32–33why, one step back
32.. _Gunicorn: https://gunicorn.org/
33.. _eventlet: https://eventlet.net/

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 34why, one step back
34

Merge branch '1.0.x'David Lordbefore adoption2236ba9

line 35why, one step back
35

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 36–43why, one step back
36uWSGI
37--------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`_::43

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 44why, one step back
44 $ uwsgi --http 127.0.0.1:5000 --module myproject:app

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 45why, one step back
45

Merge pull request #2861 from jwflory/fix/uwsgi-docsDavid Lordbefore adoption345685c

line 46why, one step back
46For a more optimized setup, see :doc:`configuring uWSGI and NGINX <uwsgi>`.

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

line 47why, one step back
47

Merge pull request #2969 from codeaditya/update-linksDavid Lordbefore adoption7e714bd

lines 48–49why, one step back
48.. _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-router

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 50–57why, one step back
50
51Gevent52-------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::57

Merge pull request #2793 from tiagocoutinho/patch-1Hsiaoming Yangbefore adoption54869a0

line 58why, one step back
58 from gevent.pywsgi import WSGIServer

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 59–75why, one step back
59 from yourapplication import app
6061 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::75

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 76why, one step back
76 $ twistd web --wsgi myproject.app

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 77–85why, one step back
77
78This 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``::85

Add prefix for all commands in documentation (#2877)Grey Libefore adoption21b0aa6

line 86why, one step back
86 $ twistd -n web --port tcp:8080 --wsgi myproject.app

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 87–131why, one step back
87
88.. _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``::131

Merge branch '1.0.x'David Lordbefore adoption2236ba9

lines 132–133why, one step back
132 from werkzeug.middleware.proxy_fix import ProxyFix
133 app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)

Merge pull request #2436 from davidism/loggingDavid Lordbefore adoptionc74f469

lines 134–155why, one step back
134
135.. 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)