Everything you need as a full stack web developer

Flask NGINX with reverse proxy configuration

- Posted in Flask by

TL;DR Flask can be used with Nginx as a reverse proxy server for high performance, scalability, and security. A reverse proxy sits between clients and web applications, routing incoming HTTP requests while improving security, enhancing performance, and allowing load balancing.

Flask, Nginx, and Reverse Proxy: A Winning Combination for Your Next Project

As a full-stack developer, you're likely no stranger to Flask, one of the most popular Python web frameworks out there. But when it comes to deploying your application in production, things can get a bit more complicated. In this article, we'll explore how to set up Nginx as a reverse proxy server for your Flask app, ensuring high performance, scalability, and security.

What is a Reverse Proxy?

A reverse proxy server sits between clients (usually browsers) and your web application, routing incoming HTTP requests from the client to your app. The benefits are numerous:

  • Improved Security: By hiding your internal IP addresses, you're less exposed to malicious attacks.
  • Enhanced Performance: Nginx can handle a massive amount of concurrent connections, offloading work from your Flask server.
  • Load Balancing: You can distribute incoming traffic across multiple instances of your app.

Setting up Flask with Nginx as a Reverse Proxy

For this example, we'll assume you have a basic Flask app set up, and you're ready to deploy it. Our setup will include:

  • Nginx installed on the same server as your Flask app
  • A separate configuration file for Nginx

Step 1: Install Nginx

On Ubuntu/Debian systems:

sudo apt-get update && sudo apt-get install nginx

On Red Hat/Fedora/CentOS systems:

sudo yum install nginx

Step 2: Configure Flask to Listen on a Specific Port

In your app.py file, change the line where you're running the development server from:

if __name__ == "__main__":
    app.run(debug=True)

to this:

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    host = os.environ.get('IP', '127.0.0.1')
    app.run(host=host, port=port, debug=False)

Step 3: Create an Nginx Configuration File

Create a new file in the /etc/nginx/sites-available/ directory (you can name it whatever you like):

sudo nano /etc/nginx/sites-available/myapp

Paste the following content into the file:

server {
    listen 80;
    server_name myapp.example.com;

    location / {
        proxy_pass http://localhost:5000; # Change this to your app's 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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Step 4: Enable the Nginx Configuration

Create a symbolic link to your new configuration file:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Step 5: Restart Nginx and Test Your App

Restart the Nginx service:

sudo service nginx restart

Visit your app in a browser at http://myapp.example.com, and you should see it up and running!

That's it! With this setup, you've successfully integrated Flask with Nginx as a reverse proxy server. You can now focus on building scalable and secure applications that take advantage of the strengths of both technologies.

In the next article, we'll explore how to add SSL encryption to your application using Let's Encrypt. Stay tuned for more exciting content from the Full Stack Developer blog!

Fullstack.ist offers meaningful insight into a broad range of topics. Fullstack.ist offers meaningful insight into a broad range of topics.
Backend Developer 102 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108