How to Use Nginx with Flask: Simple Setup Guide
To use
nginx with a Flask app, run Flask with a production server like gunicorn and configure nginx as a reverse proxy to forward client requests to the Flask server. This setup improves performance and allows nginx to handle static files and security.Syntax
This is the basic nginx server block syntax to proxy requests to a Flask app running on localhost port 8000. It listens on port 80 and forwards requests to the Flask server.
listen 80;: Nginx listens on HTTP port 80.server_name example.com;: Your domain or IP.location /: Matches all requests.proxy_pass http://127.0.0.1:8000;: Forwards requests to Flask server.proxy_set_header: Passes client info to Flask.
nginx
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;
}
}Example
This example shows a simple Flask app served by gunicorn on port 8000, with nginx configured as a reverse proxy on port 80. Requests to http://localhost/ are forwarded to Flask.
bash
# app.py from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello from Flask behind Nginx!" # Run gunicorn server (in terminal): gunicorn -w 4 -b 127.0.0.1:8000 app:app # Nginx config (e.g., /etc/nginx/sites-available/flask_app): server { listen 80; server_name localhost; 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; } } # After saving nginx config, reload nginx: sudo nginx -s reload
Output
When visiting http://localhost/ in a browser, the page shows:
Hello from Flask behind Nginx!
Common Pitfalls
Common mistakes when using nginx with Flask include:
- Not running Flask with a production server like
gunicornoruwsgi, which can cause poor performance. - Forgetting to set
proxy_set_headerdirectives, which can break client IP detection. - Not reloading
nginxafter changing configuration. - Binding Flask to
127.0.0.1but trying to access it externally withoutnginx.
Example of a wrong config missing headers:
nginx
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
# Missing proxy_set_header lines causes client info loss
}
}
# Corrected config includes proxy_set_header directives as shown in the Syntax section.Quick Reference
Tips for using nginx with Flask:
- Always run Flask with
gunicornoruwsgiin production. - Use
nginxas a reverse proxy to handle client requests and serve static files. - Set proper
proxy_set_headerdirectives to forward client info. - Reload
nginxafter config changes withsudo nginx -s reload. - Test locally before deploying to production.
Key Takeaways
Use gunicorn or uwsgi to serve Flask in production behind nginx.
Configure nginx as a reverse proxy with proxy_pass to forward requests.
Set proxy_set_header directives to preserve client information.
Reload nginx after any configuration changes.
Test the full setup locally before deploying.