0
0
FlaskHow-ToBeginner · 4 min read

How to Use Nginx with Flask: Setup and Configuration Guide

To use nginx with Flask, run your Flask app with a production server like gunicorn and configure nginx as a reverse proxy to forward client requests to the Flask app. This setup improves performance, handles static files, and adds security.
📐

Syntax

This is the basic pattern to use nginx with a Flask app:

  • Run Flask app with a WSGI server like gunicorn on a local port (e.g., 8000).
  • Configure nginx to listen on port 80 or 443 and forward requests to the Flask app's port.
  • Use proxy_pass in nginx to send requests to the Flask server.
  • Optionally serve static files directly from nginx for better speed.
nginx
server {
    listen 80;
    server_name your_domain.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;
    }

    location /static/ {
        alias /path/to/your/flask/app/static/;
    }
}
💻

Example

This example shows a simple Flask app served by gunicorn on port 8000, with nginx forwarding requests from port 80.

python and nginx
# app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello from Flask behind Nginx!'

# Run with: gunicorn -w 4 -b 127.0.0.1:8000 app:app

# nginx config snippet:
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;
    }
}
Output
When visiting http://localhost/ in a browser, you see: Hello from Flask behind Nginx!
⚠️

Common Pitfalls

Common mistakes when using nginx with Flask include:

  • Not running Flask with a production server like gunicorn and using the built-in Flask server instead, which is not for production.
  • Forgetting to set proxy_set_header directives, causing issues with client IP or HTTPS detection.
  • Incorrect proxy_pass URL or port mismatch between nginx and Flask server.
  • Not serving static files via nginx, which slows down the app.
nginx
### Wrong nginx config (missing headers):
server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

### Correct nginx config (with headers):
server {
    listen 80;
    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;
    }
}
📊

Quick Reference

Summary tips for using nginx with Flask:

  • Use gunicorn or another WSGI server to run Flask in production.
  • Configure nginx as a reverse proxy with proper headers.
  • Serve static files directly from nginx for speed.
  • Test your setup locally before deploying.

Key Takeaways

Always run Flask with a production WSGI server like gunicorn before using nginx.
Configure nginx as a reverse proxy with proxy_pass and set necessary headers.
Serve static files directly from nginx to improve performance.
Check port and URL consistency between nginx and your Flask app.
Test your nginx and Flask integration locally before deploying.