Using Nginx as Reverse Proxy
In many deployments:
- Nginx handles incoming HTTP/HTTPS traffic
- Gunicorn runs your Flask app
Why use Nginx?
Section titled “Why use Nginx?”- HTTPS termination (TLS)
- better performance for static files
- request buffering
- security headers
- reverse proxy routing
High-level flow
Section titled “High-level flow”flowchart LR U[Users] -->|HTTPS| N[Nginx] N -->|HTTP| G[Gunicorn] G -->|WSGI| F[Flask App]
Minimal Nginx config (conceptual)
Section titled “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;
}
}Static files
Section titled “Static files”Often you configure Nginx to serve /static/ directly.
That reduces load on Gunicorn.
Common gotcha
Section titled “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.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading