Nginx as a reverse proxy helps direct web traffic to your Django app safely and efficiently. It acts like a friendly gatekeeper that manages requests from users to your server.
Nginx as reverse proxy in Django
Start learning this pattern below
Jump into concepts and practice - no test required
server {
listen 80;
server_name yourdomain.com;
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;
}
}proxy_pass tells Nginx where your Django app is running (usually localhost with port 8000).
Headers like X-Real-IP help your Django app know the real user IP address.
location /static/ {
alias /path/to/your/staticfiles/;
}server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/ssl/certs/yourcert.pem;
ssl_certificate_key /etc/ssl/private/yourkey.pem;
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;
}
}This Nginx config sends all normal web requests to the Django app running on localhost port 8000. Static files are served directly from the specified folder for faster loading.
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;
}
}Remember to collect static files in Django using python manage.py collectstatic before serving them with Nginx.
Make sure your Django app is running and accessible on the port Nginx proxies to.
Use firewall rules to allow traffic only through Nginx, not directly to Django app port.
Nginx as a reverse proxy forwards user requests to your Django app safely and efficiently.
It can serve static files directly, improving speed and reducing load on Django.
It helps add HTTPS and security features easily in front of your Django app.
Practice
Solution
Step 1: Understand Nginx reverse proxy function
Nginx acts as a middleman that receives client requests and forwards them to the Django app server.Step 2: Identify what Nginx does not do
Nginx does not replace Django, execute Python code, or store backups; it only forwards requests and can serve static files.Final Answer:
Forward client requests to the Django application server -> Option CQuick Check:
Nginx forwards requests [OK]
- Thinking Nginx runs Django code
- Confusing Nginx with a database
- Assuming Nginx replaces Django app
Solution
Step 1: Identify correct proxy directive
To forward requests, Nginx usesproxy_passwith the Django app address and port.Step 2: Check other options for correctness
rootserves static files,fastcgi_passis for FastCGI apps, andproxy_redirectcontrols redirects but doesn't forward requests.Final Answer:
location / { proxy_pass http://127.0.0.1:8000; } -> Option AQuick Check:
Use proxy_pass for reverse proxy [OK]
- Using root instead of proxy_pass for proxying
- Confusing fastcgi_pass with proxy_pass
- Omitting the backend address in proxy_pass
/static/css/style.css?
location /static/ {
alias /home/user/myproject/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}Solution
Step 1: Understand alias directive for static files
Thelocation /static/block usesaliasto serve files directly from the filesystem path.Step 2: Analyze request routing
Requests to /static/ are served by Nginx from the alias path; other requests go to Django via proxy_pass.Final Answer:
Nginx serves the static file directly from /home/user/myproject/static/css/style.css -> Option AQuick Check:
Static files served by alias [OK]
- Thinking all requests go to Django
- Confusing alias with root directive
- Assuming Nginx redirects instead of serving static files
Solution
Step 1: Understand 502 Bad Gateway cause
502 means Nginx cannot connect to the backend server, often because it is not running or listening on the expected port.Step 2: Identify the correct fix
Starting the Django app server on the port Nginx expects resolves the connection issue.Final Answer:
Start the Django app server on the port Nginx proxies to -> Option BQuick Check:
502 error means backend not reachable [OK]
- Changing root instead of fixing backend server
- Removing static files block unrelated to 502
- Disabling client firewall won't fix server connection
Solution
Step 1: Serve static files with alias
Thelocation /static/block usesaliasto serve static files from the filesystem path.Step 2: Forward other requests with proxy_pass
Thelocation /block forwards all other requests to Django running on port 8000 usingproxy_pass.Final Answer:
location /static/ { alias /var/www/myproject/static/; } location / { proxy_pass http://127.0.0.1:8000; } -> Option DQuick Check:
Static files alias + proxy_pass for others [OK]
- Swapping alias and proxy_pass locations
- Using proxy_redirect instead of proxy_pass
- Using root instead of alias causing path issues
