0
0
NginxHow-ToBeginner · 4 min read

How to Use Nginx with Python: Simple Guide

To use nginx with a Python app, run your Python app with a WSGI server like Gunicorn and configure nginx as a reverse proxy to forward requests to Gunicorn. This setup improves performance and handles client connections efficiently.
📐

Syntax

This is the basic syntax to configure nginx as a reverse proxy for a Python app running on Gunicorn:

  • server: Defines the server block for your site.
  • listen: Port where nginx listens (usually 80 for HTTP).
  • location /: Matches all requests to forward.
  • proxy_pass: Forwards requests to Gunicorn running on localhost and a port.
  • proxy_set_header: Passes client info to the Python app.
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;
    }
}
💻

Example

This example shows how to run a simple Python Flask app with Gunicorn and configure nginx to serve it.

1. Create a Python app app.py.

2. Run Gunicorn to serve the app.

3. Configure nginx to proxy requests to Gunicorn.

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

@app.route('/')
def hello():
    return "Hello from Python app!"

# Run Gunicorn (command line):
gunicorn --bind 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 you visit http://localhost in your browser, you will see: Hello from Python app!
⚠️

Common Pitfalls

Common mistakes when using nginx with Python apps include:

  • Not running the Python app with a WSGI server like Gunicorn or uWSGI.
  • Incorrect proxy_pass URL or port mismatch.
  • Missing proxy_set_header directives causing client info loss.
  • Firewall blocking the port Gunicorn listens on.
  • Not restarting nginx after config changes.
nginx
Wrong nginx config example (missing proxy headers):
server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

Right nginx config example (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 Python:

  • Use Gunicorn or uWSGI to serve Python apps.
  • Configure nginx as a reverse proxy to forward requests.
  • Always set proxy headers to preserve client info.
  • Test your Python app independently before adding nginx.
  • Reload nginx after config changes with sudo nginx -s reload.

Key Takeaways

Run your Python app with a WSGI server like Gunicorn before using nginx.
Configure nginx as a reverse proxy with correct proxy_pass and headers.
Always reload nginx after changing its configuration.
Test your Python app separately to isolate issues.
Check firewall and port settings to ensure connectivity.