Skip to content

Nginx Reverse Proxy For Flask Application.

Last updated on July 17, 2023

To set up Nginx as a reverse proxy for a Flask application, you’ll need to configure Nginx to forward incoming requests to your Flask application’s server.

Here’s a step-by-step guide to help you set up nginx reverse proxy for your Flask application.

Install Nginx

  • For Ubuntu/Debian: sudo apt-get install nginx
  • For CentOS/RHEL: sudo yum install nginx

Configure the Nginx server block

  • Open the Nginx configuration file for editing:
    • For Ubuntu/Debian: sudo nano /etc/nginx/sites-available/default
    • For CentOS/RHEL: sudo nano /etc/nginx/nginx.conf

Inside the server block, remove the default configuration, and add the following

server {
    listen 80;

    location / {
        proxy_pass http://localhost:5000;  # Adjust the Flask app's server address/port
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Replace with your actual domain name. Update the proxy_pass value with the address and port of your Flask application’s server (e.g., http://localhost:5000).

Test the Nginx configuration

Run sudo nginx -t to check for any syntax errors in the configuration file.

Restart Nginx

  • For Ubuntu/Debian: sudo service nginx restart
  • For CentOS/RHEL: sudo systemctl restart nginx

Start your Flask application’s server

Make sure your Flask application is running on the address and port specified in the Nginx configuration.

With these steps completed, Nginx will act as a reverse proxy, forwarding incoming requests to your Flask application. The proxy_set_header directives in the configuration preserve important information like the client’s IP address and the original host header, which can be accessed within your Flask application.

Remember to customize the Nginx configuration and adjust the proxy settings based on your specific setup, such as enabling HTTPS, handling static files, or using custom headers. Additionally, ensure that your Flask application’s server is accessible and listening on the specified address and port.

Comments are closed, but trackbacks and pingbacks are open.