Introduction
A reverse proxy is an efficient method for an application server to access the internet. Node.js or Flask provides access to the application on the local network using TCP. In order to access the application remotely, you need to deploy a reverse proxy server. First, the client requests access to the application and receives content from the server without the necessary settings, according to the client's requirement. In this guide, you will be able to configure Nginx easily, and redirect the desired client requests. Missing a server to test applications? No problem. Consider setting up a test application on a Gunicorn WSGI server.
Preparing for installation
You need to configure Ubuntu Server 22.04 according to guidance from our website.
The server address in this guide will be called ip_address. For example, http://127.0.0.1:8000 is the standard address for the Gunicorn library. If you don't have a test application server set up, you can deploy Gunicorn, which provides the server.
Using Nginx, you can set up a domain name, when accessed by the domain name, the web browser connects to the desired address.
Stage 1 – Download and install Nginx
First, update the software package indexes and install the nginx package:
apt update
apt install nginx
Select "y" and install the package. During installation, errors related to “E: Sub-process /usr/bin/dpkg returned an error code (1)” may occur.
Open the file and comment out the line related to IPv6, save the changes and exit the file.:
vim /etc/nginx/sites-available/default
#listen [::]:80 default_server; # comment out the line and exit with ESC and :wq! By saving the file.
Run the command to correctly configure the installation package:
dpkg --configure -a
Check the functionality of the utility by running the command:
service nginx status
Allow the Nginx connection rule with ufw:
ufw allow ‘Nginx HTTP’
In the next step, we will configure the server side and test the configuration.
Stage 2 – Server side configuration
In the /etc/nginx/sites-available directory, create a file with the name of our domain:
vim /etc/nginx/sites-available/test_domain.ru;
Then add a piece of code to the file and replace test_domain.ru and ip_addres_app with your address. If there is no server to test applications then replace with http://127.0.0.1:8000 — this address is the default Gunicorn address. which we will consider in the third step.
server {
listen 80;
listen [::]:80; #if an error occurs during nginx -t check, comment out the line+
server_name test_domain.ru
www.test_domain.ru;
location / {
proxy_pass http://127.0.0.1:8000;
include proxy_params;
}
}
Save and exit by executing ESC and :wq!
The configuration starts by default listening on port 80 and will respond to test_domain.ru requests. The reverse proxy is configured using the proxy_params file in the /etc/nginx/proxy_params directory.
HTTP requests consist of headers containing information about the IP address and about cookies. The proxy_params file contains the recommended parameters:
proxy_set_header Host $http_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;
http_host — contains the initial host requested by the client, which in turn is the website domain and port;
remote_addr — the header always contains one IP address belonging to the remote host;
proxy_add_x_forwarded_for — contains the IP address of the host that sent the request. It is also possible to save a list of IP addresses. The first is the source IP address of the host, followed by a list of all the IP addresses of the reverse proxy servers through which the request passed.
scheme is a string containing the protocol that is used by the source host in order to connect. Create a symbolic link to the test_domain.ru file:
ln -s /etc/nginx/sites-available/test_domain.ru /etc/nginx/sites-enabled/
Let's test nginx with the -t switch:
nginx -t
Let's restart the service:
service nginx restart
At the moment, Nginx is configured with a reverse proxy. The next step is to install and configure the application testing server.
Stage 3 – Reverse proxy testing with Gunicorn
After starting the Nginx web server, you can check for operability using a web browser by specifying the domain name in the address bar.
Download and install the Gunicorn package, which is a Python WSGI server and runs continuously with the Nginx reverse proxy.
apt install gunicorn
It is possible to install Gunicorn directly via "pip", for the latest version, which can be linked to the Python virtual environment.
Let's write a function that will return "New WSGI server". Let's create a file:
vim wsgi.py
Let's insert a piece of code:
def wsgi(environ, start_response):
start_response("200 OK", [])
return iter([b"Welcome, to new wsgi server"])
Let's start the Gunicorn server, specify the file name without extension and the name of the function:
gunicorn --workers=2 wsgi:wsgi
#Output
[2022-11-10 18:47:16 +0000] [2633] [INFO] Starting gunicorn 20.1.0
[2022-11-10 18:47:16 +0000] [2633] [INFO] Listening at: http://127.0.0.1:8000 (2633)
[2022-11-10 18:47:16 +0000] [2633] [INFO] Using worker: sync
[2022-11-10 18:47:16 +0000] [2634] [INFO] Booting worker with pid: 2634
[2022-11-10 18:47:16 +0000] [2635] [INFO] Booting worker with pid: 2635
Open a browser and enter http://127.0.0.1:8000 and get a message.
Conclusions
With this guide you have learned:
- how to install Nginx and Gunicorn;
- add an Nginx HTTP rule;
- set up the server part for testing the application;
- create symbolic links;
- start the server to test the application.