Nginx as a reverse proxy helps your Flask app handle many users smoothly. It acts like a friendly gatekeeper that forwards requests to your app and sends back responses.
Nginx as reverse proxy in Flask
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:5000;
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;
}
}proxy_pass tells Nginx where your Flask app is running (usually localhost with port 5000).
Headers like X-Real-IP help your Flask app know the real visitor's IP address.
location /api/ {
proxy_pass http://127.0.0.1:5000/api/;
}location /static/ {
alias /var/www/static/;
}server {
listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://127.0.0.1:5000;
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;
}
}This simple Flask app runs on localhost port 5000. Nginx will forward web requests to it.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello from Flask behind Nginx!' if __name__ == '__main__': app.run(host='127.0.0.1', port=5000)
Make sure Nginx and Flask are running on the same machine or network.
Restart Nginx after changing its config with sudo systemctl restart nginx.
Check Nginx error logs if things don't work: usually at /var/log/nginx/error.log.
Nginx as a reverse proxy forwards web requests to your Flask app.
This setup improves performance, security, and scalability.
You configure Nginx with proxy_pass to point to your Flask app.
Practice
Solution
Step 1: Understand Nginx's role
Nginx acts as a middleman that receives client requests first.Step 2: Forward requests to Flask app
Nginx sends these requests to the Flask app running on a backend server or port.Final Answer:
Forward client requests to the Flask app server -> Option CQuick Check:
Nginx forwards requests = D [OK]
- Thinking Nginx runs Flask code
- Confusing Nginx with database storage
- Believing Nginx compiles Python code
Solution
Step 1: Identify directive for backend URL
The directive that tells Nginx where to send requests isproxy_pass.Step 2: Confirm other directives' roles
server_namesets domain,listensets port,rootsets file path.Final Answer:
proxy_pass -> Option AQuick Check:
Backend URL = proxy_pass [OK]
- Using server_name instead of proxy_pass for backend URL
- Confusing listen with proxy_pass
- Setting root for proxy backend
http://example.com/api/data?
location /api/ {
proxy_pass http://127.0.0.1:5000/;
}Solution
Step 1: Understand proxy_pass with trailing slash
Sinceproxy_passends with a slash, Nginx appends the full request URI after the location prefix.Step 2: Combine location and proxy_pass
Request/api/datais forwarded ashttp://127.0.0.1:5000/api/data.Final Answer:
Request is forwarded to http://127.0.0.1:5000/api/data -> Option DQuick Check:
Trailing slash keeps full path = C [OK]
- Removing /api/ from forwarded URL incorrectly
- Assuming Nginx serves files locally
- Expecting 404 without backend
location / {
proxy_pass http://127.0.0.1:8000;
}
Your Flask app runs on port 5000.Solution
Step 1: Check proxy_pass target port
proxy_pass points to port 8000, but Flask runs on 5000, so no backend listens on 8000.Step 2: Understand 502 Bad Gateway cause
502 means Nginx can't connect to backend server at specified address and port.Final Answer:
Nginx is forwarding to wrong port 8000 instead of 5000 -> Option AQuick Check:
Port mismatch causes 502 error = A [OK]
- Blaming missing trailing slash for 502
- Thinking Flask must run on 8000
- Assuming Nginx can't proxy localhost
/app/ to your Flask app at http://127.0.0.1:5000/, but remove the /app prefix before forwarding. Which configuration achieves this?Solution
Step 1: Understand proxy_pass without trailing slash
Whenproxy_passURL has no trailing slash, Nginx replaces the matching location prefix with the proxy_pass path.Step 2: Apply to /app/ location
Withlocation /app/andproxy_pass http://127.0.0.1:5000, the/app/prefix is removed before forwarding.Step 3: Check other options
The configuration withproxy_pass http://127.0.0.1:5000/;keeps /app/ prefix, the one withproxy_pass http://127.0.0.1:5000/app/;adds /app/ twice, and location without trailing slash causes mismatch.Final Answer:
location /app/ { proxy_pass http://127.0.0.1:5000; } -> Option BQuick Check:
proxy_pass without slash removes location prefix = A [OK]
- Adding trailing slash keeps prefix in forwarded URL
- Using location without trailing slash causes mismatch
- Duplicating /app/ in proxy_pass URL
Ready to prove it?
Pass the test and this pattern moves unknown
