Mastering Nginx

(Ron) #1

The NGINX HTTP Server


[ 154 ]

Wiring NGINX and uWSGI together


The Python WSGI (Web Server Gateway Interface) is an interface specification


formalized as PEP-3333 (http://www.python.org/dev/peps/pep-3333/). Its


purpose is to provide a "standard interface between web servers and Python
web applications or frameworks to promote web application portability across a


variety of web servers". Due to its popularity in the Python community, a number
of other languages have implementations that conform to the WSGI specification.


The uWSGI server, although not written exclusively for Python, provides a way of


running applications that conform to this specification. The native protocol used
to communicate with the uWSGI server is called uwsgi. More details about the


uWSGI server, including installation instructions, example configurations, and
other supported languages can be found at http://projects.unbit.it/uwsgi/


and https://github.com/unbit/uwsgi-docs.


NGINX's uwsgi module can be configured to talk to this server using directives


similar to the fastcgi_* directives discussed in the previous section. Most directives
have the same meaning as their FastCGI counterparts, with the obvious difference


being that they begin with uwsgi instead of fastcgi. There are a few exceptions


however—uwsgi_modifier1 and uwsgi_modifier2, as well as uwsgi_string.
The first two directives set either the first or second modifier, respectively, of the


uwsgi packet header. uwsgi_string enables NGINX to pass an arbitrary string to
uWSGI, or any other uwsgi server that supports the eval modifier. These modifiers


are specific to the uwsgi protocol. A table of valid values and their meanings can be


found at http://uwsgi-docs.readthedocs.org/en/latest/Protocol.html.


An example Django configuration


Django (https://www.djangoproject.com/) is a Python web framework in which
developers can quickly create high-performing web applications. It has become a


popular framework in which many different kinds of web applications are written.


The following configuration is an example of how to connect NGINX to multiple


Django applications running under an Emperor mode uWSGI server with FastRouter
activated. See the URLs embedded in the comments in the following code for more


information about running uWSGI like this:


http {
# spawn a uWSGI server to connect to
# uwsgi --master --emperor /etc/djangoapps --fastrouter
127.0.0.1:3017 --fastrouter-subscription-server 127.0.0.1:3032
Free download pdf