Django is a powerful framework for building websites. To run a production website, usually an application server is used. So nginx will do two basic things:
- Serve your Django application from the application server port to the web port (Reverse Proxy)
- Serve static and media files
The application server used in this example is gunicorn, the application server chosen by Instagram of the earlier days, but it can be anything running on port 9999. Change port number as required in the example.
The following nginx conf was adapted from this, with some additions and it contains:
- a commented non www to www website redirect
- media files with etag (1 year)
- static files with etag (1 minute)
- an host-based favicon distributor (reusable as is)
- a commented basic auth to make a website private
- reverse proxy to gunicorn
- a simple block for a common type of malicious activity
It works fine with Django 1 and 2.
Run nginx -t to check and then systemctl reload nginx to apply.
This is a http version, to configure the website for https follow this howto.