ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

Configure a Reverse Proxy with Nginx (Python)

Configure a Reverse Proxy with Nginx (Python)
Configure a Reverse Proxy with Nginx (Python)

In this article, you will also learn how to create a simple page in Python using the framework Flask and make it available using the web server Gunicorn. Simply you’re going to configure a Reverse Proxy with Nginx.

Prerequisites:

  • What is Nginx and how does it works
  • What is a reverse proxy
  • The basics of a web page in Python
  • How to deploy an application using Gunicorn

Installing Nginx

It is also one of the most famous web servers, more often used as a load balancer or as a proxy pass. It is frequently used with Python applications. It is usually used with another Python server like Gunicorn.

To begin with, let’s install Nginx (use sudo if required):

$ apt install nginx -y

After the installation finishes, you can check all the configuration files in this folder by the
following command:

$ ls /etc/nginx/

Output:

Reverse Proxy with Nginx.
List of files inside /etc/nginx/ directory

Here most important is the nginx.conf file, that stores the main configuration of the web server.

Now, start and enable Nginx:

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Open IP address(If your server is in VM or in Cloud) or localhost you’ll see Nginx welcome page.

Install Python

To install python run the following command:

$ apt install python3 python3-pip -y

Now, install Flask framework using pip:

$ python3 -m pip install flask

Now, you’re ready to run a python application.

Let’s create a simple page using Flask:

Simple Page Using Flask

Flask is a micro-framework for Python, which is perfect for you to create your APIs. Now, you will learn how to create a simple Hello World using this micro-framework and Python. You will also learn all the steps to deploy your application using Nginx and Gunicorn. Let’s create a file, called app.py with the following content:

from flask import Flask 


app = Flask(__name__) 

@app.route("/") 
def index():
	return "Reverse Proxy using Nginx" 
if __name__ == "__main__": 
	app.run(debug=True,host="0.0.0.0")

Now, run the Flask application:

$ python3 app.py

You can see the server is running, get the IP address, and access from the browser. You must see a page as:

Since it’s a development server, you must use a WSGI(Web Server Gateway Interface). The WSGI server we’ll be using is Gunicorn. To install, run the following command:

$ python3 -m pip install gunicorn

Now, we can deploy the application again, but using the WSGI server. To do this, run the
following command:

$ gunicorn --chdir /root/ app:app -b "0.0.0.0"

Here, a noticeable thing is the port number, which is changed to 8000.

It’s so simple like that. However, we also want to run this application behind a reverse proxy. So, let’s see how we are going to configure that.

Configure the Reverse Proxy with Nginx

We have already installed Nginx in our VM and we also know the configuration files. Knowing
that, the configuration for the pages can be found in the virtual hosts which are found in the
directory:

$ ls /etc/nginx/sites-available/default

Then, we can open the file and your file should have the content as follows:

server { 
	listen 80 default_server; 
	listen [::]:80 default_server; 
	server_name _; 
	location / { 
	proxy_pass http://localhost:8000/ ;
	} 
}

Restart the server to enable the configuration:

$ sudo systemctl restart nginx

It happens because the server behind our proxy is not running yet. So, let’s run it and check the changes:

$ gunicorn --chdir /root/ app:app

This time we do not need to pass the bind option, -b to make the server available to all the IP
addresses. In the current case, the server is just listening to the localhost. Therefore, Nginx is
open to the world receiving the connections and forwarding them to Gunicorn.

Conclusion

The environment is ready. Reverse proxies are used for many strategies. One of the most
common is caching static files, like JavaScript, CSS, and HTML. Therefore, all the incoming
connections are responded to by Nginx and only the dynamic content is forwarded to Gunicorn. This way is less costly for the CPU to process everything.

Sign up for daily dose of tech articles at your inbox.
Loading