When you deploy a Django, Flask or Spring Boot app, it usually does not face the internet directly. A web server called Nginx sits in front of it as a reverse proxy. Knowing the basics is expected of DevOps and full stack developers.
What a reverse proxy does
- Receives requests from browsers on ports 80 (HTTP) and 443 (HTTPS).
- Forwards them to your app running on an internal port, such as 8000 or 8080.
- Handles HTTPS certificates, so your app does not have to.
- Serves static files (CSS, images) quickly without touching the app.
- Can spread traffic across several app instances (load balancing).
A basic configuration
server {
listen 80;
server_name example.com;
location /static/ {
alias /var/www/app/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
The headers pass the original host, visitor IP and protocol to your app, which needs them for correct links and logs.
Everyday commands
sudo nginx -t: test the configuration before applying it. Always run this first.sudo systemctl reload nginx: apply changes without dropping connections.sudo tail -f /var/log/nginx/error.log: watch errors.
HTTPS
Free certificates from Let’s Encrypt, installed with Certbot, add HTTPS in a few minutes and renew automatically. Redirect all HTTP traffic to HTTPS once it works.
Common errors
- 502 Bad Gateway: Nginx cannot reach your app. Check that the app is running on the port in proxy_pass.
- 404 for static files: wrong path in alias or root.
- 413 Request Entity Too Large: raise
client_max_body_sizefor file uploads.
Related: deploying a Python web app, Docker Compose and Linux commands for DevOps.
Hands-on labs like this are part of our AWS and DevOps course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
