How to Use Nginx with Django: Setup and Configuration Guide
To use
nginx with Django, set up Gunicorn as the application server to run Django, then configure nginx as a reverse proxy to forward web requests to Gunicorn. This setup improves performance and handles static files efficiently.Syntax
This is the basic syntax to configure nginx as a reverse proxy for a Django app served by Gunicorn:
- server: Defines the web server block.
- listen: Port number to listen on (usually 80 for HTTP).
- server_name: Your domain or IP address.
- location /static/: Serves static files directly from disk.
- location /: Proxies requests to Gunicorn running on a local port.
nginx
server {
listen 80;
server_name example.com;
location /static/ {
alias /path/to/your/staticfiles/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Example
This example shows a full setup where Django runs with Gunicorn on port 8000, and Nginx listens on port 80 to serve static files and forward other requests to Gunicorn.
bash
# 1. Run Gunicorn to serve Django app # Run this command inside your Django project directory gunicorn myproject.wsgi:application --bind 127.0.0.1:8000 # 2. Nginx configuration file (/etc/nginx/sites-available/myproject) server { listen 80; server_name example.com; location /static/ { alias /home/user/myproject/static/; } location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } # 3. Enable the Nginx site and restart Nginx # sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/ # sudo systemctl restart nginx
Output
Nginx listens on port 80 and forwards requests to Gunicorn on port 8000; static files are served directly by Nginx from /home/user/myproject/static/
Common Pitfalls
Common mistakes when using Nginx with Django include:
- Not running Gunicorn or running it on the wrong port, so Nginx cannot connect.
- Incorrect
aliaspath for static files causing 404 errors. - Missing proxy headers, which can break Django’s request handling.
- Forgetting to collect static files with
python manage.py collectstatic.
Always test your Nginx config with nginx -t before restarting.
nginx
## Wrong static alias example (causes 404): location /static/ { alias /wrong/path/static/; } ## Correct static alias example: location /static/ { alias /home/user/myproject/static/; }
Quick Reference
Tips for using Nginx with Django:
- Use Gunicorn as the WSGI server for Django.
- Configure Nginx to serve static files directly for better performance.
- Set proper proxy headers in Nginx to preserve client info.
- Run
collectstaticto gather static files before deployment. - Test Nginx config with
nginx -tand reload withsystemctl restart nginx.
Key Takeaways
Use Gunicorn to serve your Django app and Nginx as a reverse proxy for better performance.
Configure Nginx to serve static files directly using the correct alias path.
Always set proxy headers in Nginx to forward client information to Django.
Run python manage.py collectstatic before deploying to gather static files.
Test your Nginx configuration with nginx -t before restarting the service.