Nginx

Nginx, pronounced "engine-X", is the second most common web server among the top 100,000 websites. Nginx also functions well as a reverse proxy to handle requests and pass back responses for Python WSGI servers or even other web servers such as Apache.

Official Nginx logo.

How is Nginx used in a Python web app deployment?

Nginx is commonly used as a web server to serve static assets such as images, CSS and JavaScript to web browser clients.

Nginx is also typically configured as a reverse proxy, which passes appropriate incoming HTTP requests to a WSGI server. The WSGI server produces dynamic content by running Python code. When the WSGI server passes its response, which is often in the HTML, JSON or XML format, the reverse proxy then responds to the client with that result.

The request and response cycle with a reverse proxy server and the WSGI server can be seen in the following diagram.

Python web application deployments rely on Nginx either as a web server or reverse proxy for WSGI servers.

Typically the client will not know or need to know that a Python web application generated the result. The result could have instead been generated by one or more backend systems written in any programming language, not just Python.

Nginx is an implementation of the web server concept. Learn how these pieces fit together in the deployment chapter or view the table of contents for all topics.

Should I use Nginx or the Apache HTTP Server?

Let's be clear about these two "competing" servers: they are both fantastic open source projects and either will serve your web application deployment well. In fact, many of the top global web applications use both servers in their deployments to function in many steps throughout the HTTP request-response cycle.

I personally use Nginx more frequently than Apache because Nginx's configuration feel easier to write, with less boilerplate than alternatives.

There's also a bit of laziness in the usage: Nginx works well, it never causes me problems. So I stick with my battle-tested Ansible configuration management files that set up Nginx with HTTPS and SSL/TLS certificates

Securing Nginx

Nginx's default configuration after a standard installation through a system package manager or compiling from source is a good base for security. However, setting up ciphers and redirects can be confusing the first few times you try it. It's a really good idea to read some of these tutorials to make sure you are avoiding the most common security errors that plague HTTP(S) configurations.

Specific Nginx resources

Nginx can be used without Python so there are a massive number of fantastic resources available for installing, configuring and optimizing this web server implementation. The following resources are ones that I collected during my own struggle while learning how to use Nginx after I had used Apache HTTP Server for several years.

Nginx release summaries

Continue learning about web servers or move to a new topic?

Which web server should I use?

What runs a Python application execute on the server?

How should I host and serve static content files?

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.


Matt Makai 2012-2018