Redirect IP and Port URL to Flask API with Nginx
This guide shows you how to configure Nginx as a reverse proxy so that a URL like http://192.168.1.10:5000 (or a domain name) forwards requests to your Flask API running on a different port. This is useful for hiding the Flask development server, enabling HTTPS, and improving performance. You’ll need a Linux server with Nginx installed, your Flask app running on a specific port (e.g., 5000), and sudo access.
What You’ll Need
- A server running Linux (Ubuntu/Debian used in examples) with Nginx installed (
sudo apt install nginx) - Your Flask app running locally on a port (e.g., 5000) – often via
flask runor Gunicorn - Root or sudo access to edit Nginx configuration files
- Basic familiarity with the command line
Step-by-Step
- Ensure Flask is running – Start your Flask app on a known port, e.g.,
flask run --host=0.0.0.0 --port=5000. Verify it responds locally:curl http://127.0.0.1:5000. - Create an Nginx server block – Create a new configuration file:
sudo nano /etc/nginx/sites-available/flask_app. - Write the server block – Add the following configuration, replacing
your_server_ip_or_domainwith your server’s IP or domain, and5000with your Flask port:server { listen 80; server_name your_server_ip_or_domain; 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; } } - Enable the site – Create a symbolic link:
sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled/ - Test Nginx configuration – Run
sudo nginx -tto check for syntax errors. If successful, you’ll see “syntax is ok” and “test is successful”. - Restart Nginx – Apply changes with
sudo systemctl restart nginx(orsudo service nginx restart). - Test the redirect – In a browser, go to
http://your_server_ip(no port needed). You should see your Flask app’s response.
Common Mistakes
- Binding Flask to localhost only – If Flask runs on
127.0.0.1without--host=0.0.0.0, Nginx can still proxy to it (since both are on the same machine), but make sure the port is open. - Using the wrong proxy_pass URL – Ensure you include the protocol (
http://) and the correct port. A missing trailing slash can cause path issues; usehttp://127.0.0.1:5000(no trailing slash) for the root location. - Forgetting to enable the site – The symlink in
sites-enabledis required; otherwise Nginx ignores your file. - Firewall blocking port 80 – If you can’t access the site, check that port 80 is open in your firewall (e.g.,
sudo ufw allow 80/tcp). - Not restarting Nginx – Changes only take effect after a restart or reload (
sudo systemctl reload nginx).
Tips
- Use a Unix socket instead of TCP for better performance: set
proxy_pass http://unix:/tmp/flask.sock;and configure Gunicorn to bind to that socket. - For production, run Flask under Gunicorn or uWSGI, not the built-in development server. Example:
gunicorn -w 4 -b 127.0.0.1:5000 app:app. - Add HTTPS with Let’s Encrypt (Certbot) – Nginx can handle SSL termination, and your Flask app stays on HTTP internally.
- If you have multiple Flask apps, create separate server blocks with different
server_nameorlocationpaths. - Log Nginx access and error logs to debug:
sudo tail -f /var/log/nginx/error.log.
Troubleshooting
If the redirect doesn’t work, follow these steps:
- Check Nginx is running –
sudo systemctl status nginx. If it’s not active, start it. - Verify Flask is listening –
sudo netstat -tulpn | grep 5000(orss -tulpn). You should see a LISTEN entry. - Test Nginx locally –
curl -H "Host: your_server_ip" http://127.0.0.1– if you get a response, Nginx is working; the issue is external (firewall/DNS). - Check Nginx error logs –
sudo tail -f /var/log/nginx/error.log– look for “connection refused” (Flask not running) or “permission denied” (socket issues). - If you get a 502 Bad Gateway – This usually means Nginx can’t reach Flask. Ensure Flask is running and the proxy_pass port matches.
- If you get a 404 – The Flask app might be expecting a different URL path. Check your Flask routes and the
locationblock in Nginx.
Frequently Asked Questions
Why use Nginx instead of just accessing Flask directly?
Nginx handles static files, load balancing, SSL, and security better than Flask’s development server. It also allows multiple apps on the same server.
Can I use a domain name instead of an IP?
Yes, set server_name to your domain and ensure DNS points to your server. You may need to configure a virtual host.
What if my Flask app uses WebSockets?
Add proxy_http_version 1.1; and proxy_set_header Upgrade $http_upgrade; and proxy_set_header Connection "upgrade"; in the location block.
How do I redirect only a specific path, like /api?
Use location /api { proxy_pass http://127.0.0.1:5000; }. Note that the path is passed as-is; use a trailing slash on proxy_pass if you want to strip the prefix.
Is it safe to expose Flask directly to the internet?
No, the built-in server is not designed for production. Always use a reverse proxy like Nginx and run Flask under a WSGI server.
How can I make the redirect permanent (301)?
Add return 301 http://your_server_ip$request_uri; in a separate server block that listens on the old port, or use proxy_pass with a permanent redirect if you’re changing domains.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.