Using Nginx is a widespread and highly recommended option for its performance, flexibility and ability to secure connections. To set up this solution, simply install Nginx, create a configuration file and adapt the server settings. In addition, you can test the configuration of your Reverse Proxy using Gunicorn.
Install and configure a reverse proxy Nginx under Ubuntu 22.04
To have a Secure, flexible internet server and little -resourcesthe use of a proxy reverse can be wise. This is inserted between the customer and the Ubuntu server, without visitors realizing the use of the request mediator. Also, is the Nginx Reverse Proxy a very appreciable solution for all incoming requests. Let's discover together how to install it and configure it under Ubuntu 22.04.
What are the prerequisites for a successful installation?
To be able to configure a reverse proxy Nginx under Ubuntu 22.04, the following elements are essential:
- An Ubuntu server installed and configured
- IP address or UNIX Domain Socket of the server
- The domain of your server
- Privileges
sudo
for the server
Managed NextCloud of Ionos Cloud
Work as a team in your own cloud
- Data security
- Integrated collaboration tools
- Accommodation in European data centers
First, update your repository via terminal in order to have Access to the latest packages. Then install Nginx with the command apt install
. Here is the appropriate code:
$ sudo apt update
$ sudo apt install nginx
bash
Confirm with [Y] and press [Enter] To validate the parameters.
In the next step, configure your firewall so that Nginx can access your server. To do this, add an exception with the following command:
$ sudo ufw allow 'Nginx HTTP'
bash
Finally, check that the installation of Nginx has succeeded. Enter the following command:
$ systemctl status nginx
bash
If Nginx has been installed correctly, the following result will be generated:
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-06-24 06:52:46 UTC; 39min ago
Docs: man:nginx(8)
Main PID: 9919 (nginx)
Tasks: 2 (limit: 2327)
Memory: 2.9M
CPU: 50ms
CGroup: /system.slice/nginx.service
├─9919 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─9920 "nginx: worker process"
bash
It is now a question of configuring your server block to prepare the Nginx Reverse Proxy optimally for your system. To do this, create and open a New configuration file with the text editor Nano. Enter the following command, replacing « your_doma » by the real name of your domain:
$ sudo nano /etc/nginx/sites-available/votre_domaine
bash
Once the file is opened, insert the following content. Replace « your_domaine » and « Address_server » with the Domaine and the IP or the Unix Domain Socket of your server:
server {
listen 80;
listen [::]:80;
server_name votre_domaine www.votre_domaine;
location / {
proxy_pass http://server_adresse;
include /etc/nginx/proxy_params;
}
}
TXT
Save and close the file. The content displayed is a typical configuration For a proxy reverse with Nginx. Port 80 is used to react to your domain and server requests. The component proxy_pass
is essential for Nginx to work as a proxy reverse. If necessary, you can also configure additional servers.
Then create a link to the repertoire sites-enabled
to which Nginx accesses at the beginning. To do this, use the following command by replacing again « your_domain »:
$ sudo ln -s /etc/nginx/sites-available/votre_domaine/ etc/nginx/sites-enabled/
bash
Check that your configuration does not include errors:
If no error message is displayed, you can restart the Nginx Reverse Proxy to confirm all the settings. To do this, enter the following command:
$ sudo systemctl restart nginx
bash
The configuration of Nginx as a Reverse Proxy is now over. In the next section, we will see you how to test the proxy. However, this test is not compulsory.
To test your Reverse Proxy Nginx, you can, in principle, use your server. Just call it via the shell. You can also use the Internet server HTTP Gunicorn which combines very well with the Nginx Reverse Proxy. First update the packages and install the server:
$ sudo apt update
$ sudo apt install gunicorn
bash
Then create a simple function that will be sent to your browser in the form of a HTTP response. Use nano again for this purpose:
Open the file and enter the following code:
def app(environ, start_response):
start_response("200 OK", [])
return iter([b"Ceci est un test"])
TXT
Then save and close the file, launch Gunicorn and call the test module:
$ gunicorn --worker=2 test:app
bash
The result should look like this:
[2024-06-24 07:09:29 +0000] [10568] [INFO] Starting gunicorn 20.1.0
[2024-06-24 09:14:37 +0000] [10568] [INFO] Listening at: http://127.0.0.1:8000 (10568)
[2024-06-24 09:14:37 +0000] [10568] [INFO] Using worker: sync
[2024-06-24 09:14:37 +0000] [10569] [INFO] Booting worker with pid: 10569
[2024-06-24 09:14:37 +0000] [10570] [INFO] Booting worker with pid: 10570
bash
This is the confirmation that Gunicorn interacts with the default address http://127.0.0.1:8000
. To finish, Open your browser And access the area you have configured with Nginx. Now the Nginx Reverse Proxy displays the following message: « This is a test ».