============
WSGI Servers
============

:category: page
:slug: wsgi-servers
:sort-order: 07

A `Web Server Gateway Interface <http://wsgi.readthedocs.org/en/latest/>`_
(WSGI) server implements the web server side of the WSGI interface for
running Python web applications. The WSGI standard v1.0 is specified in
`PEP 0333 <http://www.python.org/dev/peps/pep-0333/>`_. As of September 2010,
WSGI v1.0 is superseded by
`PEP 3333 <http://www.python.org/dev/peps/pep-3333/>`_, which defines the
v1.0.1 WSGI standard.


.. image:: theme/img/web-browser-server-wsgi.png
  :alt: WSGI Server <-> Web server <-> Browser
  :width: 100%
  :class: technical-diagram

A web server's configuration specifies what requests should be passed to
the WSGI server to process. Once a request is processed and generated by the
WSGI server, the response is passed back through the web server and onto
the browser. For example, this Nginx web server's configuration specifics
Nginx should handle static assets (such as images, JavaScript, and CSS
files) under the /static directory and pass all other requests to the WSGI
server running on port 8000::

    # this specifies that there is a WSGI server running on port 8000
    upstream app_server_djangoapp {
        server localhost:8000 fail_timeout=0;
    }

    # Nginx is set up to run on the standard HTTP port and listen for requests
    server {
      listen 80;

      # nginx should serve up static files and never send to the WSGI server
      location /static {
        autoindex on;
        alias /srv/www/assets;
      }

      # requests that do not fall under /static are passed on to the WSGI
      # server that was specified above running on port 8000
      location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
          proxy_pass http://app_server_djangoapp;
          break;
        }
      }
    }

Note that the above code is a simplified version of a production-ready Nginx
configuration. For real SSL and non-SSL templates, take a look at the
`Underwear web server templates <https://github.com/makaimc/underwear/tree/master/underwear/roles/web/templates>`_ on GitHub.

WSGI is by design a simple standard interface for running Python code. As
a web developer you won't need to know much more than

* what WSGI stands for (Web Server Gateway Inteface)

* that a WSGI container is a separate running process that runs on a
  different port than your web server

* your web server is configured to pass requests to the WSGI container which
  runs your web application, then pass the response (in the form of HTML)
  back to the requester

If you're using a standard web framework such as Django, Flask, or
Bottle, or almost any other current Python framework, you don't need to worry
about how frameworks implement the application side of the WSGI standard.
Likewise, if you're using a standard WSGI container such as Green Unicorn,
uWSGI, mod_wsgi, or gevent, you can get them running without worrying about
how they implement the WSGI standard.

However, knowing the WSGI standard and how these frameworks and containers
implement WSGI should be on your learning checklist though as you become
a more experienced Python web developer.


WSGI's Purpose
--------------
Why use WSGI and not just point a web server directly at an application?

* **WSGI gives you flexibility**. Application developers can swap out
  web stack components for others. For example, a developer can switch from 
  Green Unicorn to uWSGI without modifying the application or framework 
  that implements WSGI. 
  From `PEP 3333 <http://www.python.org/dev/peps/pep-3333/>`_:

    The availability and widespread use of such an API in web servers for 
    Python [...] would separate choice of framework from choice of web 
    server, freeing users to choose a pairing that suits them, while 
    freeing framework and server developers to focus on their preferred 
    area of specialization.

* **WSGI servers promote scaling**. Serving thousands of requests for dynamic
  content at once is the domain of WSGI servers, not frameworks.
  WSGI servers handle processing requests from the web server and deciding
  how to communicate those requests to an application framework's process.
  The segregation of responsibilities is important for efficiently scaling 
  web traffic.


WSGI Resources
--------------
`PEP 0333 WSGI v1.0 <http://www.python.org/dev/peps/pep-0333/>`_
and
`PEP 3333 WSGI v1.0.1 <http://www.python.org/dev/peps/pep-3333/>`_
specifications.

`Green Unicorn <http://gunicorn.org/>`_,
`mod_wsgi <http://code.google.com/p/modwsgi/>`_,
`uWSGI <https://github.com/unbit/uwsgi-docs>`_, and
`gevent <http://www.gevent.org/>`_ are common WSGI server implementations.

This `Basics of WSGI <http://agiliq.com/blog/2013/07/basics-wsgi/>`_ post
contains a simple example of how a WSGI-compatible application works.

`Complete single server Django stack tutorial <http://www.apreche.net/complete-single-server-django-stack-tutorial/>`_ is thorough and informative for
non-paas hosting choices.

This detailed post entitled
`The Beautiful Simplicity of an nginx and uWSGI Deployments <http://bartek.im/blog/2012/07/08/simplicity-nginx-uwsgi-deployment.html>`_
is great reading for understanding Nginx and uWSGI configurations.

