Using NGINX and NGINX Plus as an Application Gateway with uWSGI and Django (original) (raw)
- Home
- F5 NGINX Plus
- Admin Guide
- Web Server Using NGINX and NGINX Plus as an Application Gateway with uWSGI and Django
This article explains how to use NGINX or F5 NGINX Plus as an application gateway with uWSGI and Django.
NGINX is a high‑performance, scalable, secure, and reliable web server and a reverse proxy. NGINX uses web acceleration techniques to manage HTTP connections and traffic. Features like load balancing, SSL termination, connection and request policing, static content offload, and content caching help users build reliable websites.
NGINX acts as a secure application gateway, passing traffic from users to applications. In this regard, not only can NGINX proxy HTTP and HTTPS traffic to an HTTP‑enabled application container, it can also connect to most of the popular application servers and web frameworks via optimized app‑gateway interfaces implemented in modules like FastCGI, Memcached, scgi, and uwsgi.
Many application containers have embedded external HTTP interfaces with some routing capabilities. NGINX offers an all‑in‑one solution. It handles HTTP connection management, load balancing, content caching, and traffic security. The application backend sits behind NGINX for better scalability and performance. You can group app instances behind NGINX to ensure high availability.
A few words about “specialized interfaces.” As useful as it is, HTTP has never been designed for modern, lightweight application‑deployment scenarios. Over time, standardized interfaces have evolved for use with various application frameworks and application containers. One of these interfaces is the Web Server Gateway Interface (WSGI), an interface between a web server/proxy and Python‑based applications.
One common application server is the uWSGI application server container. It offers uwsgi - its own implementation of the WSGI protocol.
Other than that, the uWSGI application server supports HTTP, FastCGI, and SCGI – with the uwsgi protocol recommended as the fastest way to talk to applications.
Configure NGINX and NGINX Plus for Use with uWSGI and Django
This document provides an example of how to configure NGINX and NGINX Plus for use with a uWSGI server and a Python development environment.
NGINX 0.8.40 and later (and all releases of NGINX Plus) includes native support for passing traffic from users to Python applications via the uwsgi protocol. If you downloaded NGINX Open Source binaries or source from our official repositories, or NGINX Plus from the customer portal, no action is needed to enable support for the uwsgi protocol – NGINX and NGINX Plus support uswgi by default.
Configuring the uWSGI application container itself is outside the scope of this document; refer to the excellent Quickstart for Python/WSGI applications for more information.
Django is a common Python web framework. For simplicity the example uses a Django‑based setup for the Python app. The Django documentation provides extensive information on how to configure a Django environment.
This example is illustrative only, and one way you might invoke your uWSGI server with Django:
--chdir=/var/django/projects/myapp \
--module=myapp.wsgi:application \
--env DJANGO_SETTINGS_MODULE=myapp.settings \
--master --pidfile=/usr/local/var/run/uwsgi/project-master.pid \
--socket=127.0.0.1:29000 \
--processes=5 \
--uid=505 --gid=505 \
--harakiri=20 \
--max-requests=5000 \
--vacuum \
--daemonize=/usr/local/var/log/uwsgi/myapp.log
--chdir=/var/django/projects/myapp \
--module=myapp.wsgi:application \
--env DJANGO_SETTINGS_MODULE=myapp.settings \
--master --pidfile=/usr/local/var/run/uwsgi/project-master.pid \
--socket=127.0.0.1:29000 \
--processes=5 \
--uid=505 --gid=505 \
--harakiri=20 \
--max-requests=5000 \
--vacuum \
--daemonize=/usr/local/var/log/uwsgi/myapp.log
With these options in place, here’s a sample NGINX configuration for use with a Django project:
nginx
http {
# ...
upstream django {
server 127.0.0.1:29000;
}
server {
listen 80;
server_name myapp.example.com;
root /var/www/myapp/html;
location / {
index index.html;
}
location /static/ {
alias /var/django/projects/myapp/static/;
}
location /main {
include /etc/nginx/uwsgi_params;
uwsgi_pass django;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}
}
}
http {
# ...
upstream django {
server 127.0.0.1:29000;
}
server {
listen 80;
server_name myapp.example.com;
root /var/www/myapp/html;
location / {
index index.html;
}
location /static/ {
alias /var/django/projects/myapp/static/;
}
location /main {
include /etc/nginx/uwsgi_params;
uwsgi_pass django;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}
}
}
Notice that the configuration defines an upstream called Django, with a server containing the port number 29000
. It matches the port specified by the socket
argument in the sample uwsgi
command. The uWSGI server binds to this port.
NGINX or NGINX Plus serves static files from /var/django/projects/myapp/static. NGINX sends /main traffic to the Django app by converting it from HTTP to the uWSGI protocol.
Lightweight, heterogeneous application environments are becoming a popular way of building and deploying modern web applications. Newer, standardized application interface protocols like uwsgi and FastCGI enable faster communication between users and applications.
Using NGINX and NGINX Plus in front of an application container has become a common way to free applications from the burden of HTTP traffic management, and to protect the application from unexpected spikes of user traffic, malicious behavior, denial‑of‑service (DoS) attacks, and more. This allows developers to fully focus on the application logic, and leave the web acceleration and fundamental HTTP traffic security tasks to NGINX or NGINX Plus.
- NGINX support in the uWSGI project documentation
- How to use Django with uWSGI in the Django project documentation