How to set up an NGINX reverse proxy on Ubuntu 22.04
Using NGINX as a reverse proxy is a popular option. To set it up, all you have to do is install NGINX, create a configuration file and adjust your server settings. You can also test whether the setup was successful with Gunicorn.
How to set up NGINX as a reverse proxy
Reverse proxies can help increase security, flexibility and resource availability. They are placed in between the client and the server and aren’t detectible to users. The NGINX reverse proxy is a highly recommended solution for incoming requests. Below we’ll explain how to install it and set it up on Ubuntu 22.04.
We also have instructions for installing NGINX on Ubuntu 20.04. And you can read more about what a proxy server is here.
What are the system requirements for NGINX as a reverse proxy?
To set up an NGINX reverse proxy on Ubuntu 22.04, you’ll need the following:
- A fully configured Ubuntu server
- The server’s IP address or Unix domain socket
- The domain of your server
sudo
privileges for the server
Installing NGINX as a reverse proxy
First, update your repository using the terminal, in order to get access to the latest packages. Then install NGINX using the apt install
command. Here is the code:
$ sudo apt update
$ sudo apt install nginx
bashConfirm with [Y] and press [Enter] to apply the settings.
Next, configure your firewall so that NGINX has access to your server. You can use the following command to add an exception:
$ sudo ufw allow 'Nginx HTTP'
bashThen check that the installation was successful:
$ systemctl status nginx
bashIf NGINX was installed properly, you’ll get output that looks like this:
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"
bashCreating the configuration file and adapting the server
Now you can configure your server block to prepare the NGINX reverse proxy for your system. To do that, create and open a new configuration file with the text editor Nano. Enter the following command, and be sure to replace the placeholder “your_domain” with the name of your domain:
$ sudo nano /etc/nginx/sites-available/your_domain
bashWhen the file is open, add the following content. Replace the placeholders “your_domain” and “server_address” with the domain and IP or Unix domain socket for your server:
server {
listen 80;
listen [::]:80;
server_name your_domain www.your_domain;
location / {
proxy_pass http://server_address;
include /etc/nginx/proxy_params;
}
}
txtSave and close the file. The content shown is the default setup for NGINX. It uses port 80 to respond to requests from your domain and server. proxy_pass
is an essential component for NGINX’s role as a reverse proxy. You can also set up additional servers if needed.
Next, create a link to the sites-enabled
directory that NGINX accesses at the beginning. Use the following command and don’t forget to replace the “your_domain” placeholder:
$ sudo ln -s /etc/nginx/sites-available/your_domain/etc/nginx/sites-enabled/
bashCheck your configuration for errors:
$ sudo nginx -t
bashIf you don’t receive any error messages, you can now restart the NGINX reverse proxy to apply the settings. To do that, enter the following command:
$ sudo systemctl restart nginx
bashYou’re now done configuring NGINX as a reverse proxy. In the next section, we explain how to check the proxy. That testing is, however, optional.
Checking NGINX reverse proxy with Gunicorn
If you choose to test your NGINX reverse proxy, you can usually use your server for that. If you use your server, open it using the shell. Alternatively, you can use the lean HTTP web server Gunicorn, which works very well with an NGINX reverse proxy. First update the packages and install the server:
$ sudo apt update
$ sudo apt install gunicorn
bashThen create a simple function to send as an HTTP response to your browser. Use nano once again:
$ nano test.py
bashOpen the file and enter the following code:
def app(environ, start_response):
start_response("200 OK", [])
return iter([b"This is a test"])
txtThen save and close the file. Afterwards, start Gunicorn and open the test module:
$ gunicorn --worker=2 test:app
bashThe output should look more or less 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
bashThat’s the confirmation that Gunicorn is interacting with the address http://127.0.0.1:8000
. Finally, open your browser and go to the domain you configured with NGINX. The NGINX reverse proxy will show the message “This is a test”.