Using Nginx as Reverse Proxy
In many deployments:
- Nginx handles incoming HTTP/HTTPS traffic
- Gunicorn runs your Flask app
Why use Nginx?
- HTTPS termination (TLS)
- better performance for static files
- request buffering
- security headers
- reverse proxy routing
High-level flow
false
flowchart LR U[Users] -->|HTTPS| N[Nginx] N -->|HTTP| G[Gunicorn] G -->|WSGI| F[Flask App]
false
Minimal Nginx config (conceptual)
server {
listen 80;
server_name example.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;
}
}server {
listen 80;
server_name example.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;
}
}Static files
Often you configure Nginx to serve /static//static/ directly.
That reduces load on Gunicorn.
Common gotcha
If you build absolute URLs, make sure Flask knows itβs behind HTTPS.
Forwarded headers (X-Forwarded-Proto) and proper proxy settings matter.
If this helped you, consider buying me a coffee β
Buy me a coffeeWas this page helpful?
Let us know how we did
