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:
Confirm 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:
Then check that the installation was successful:
If NGINX was installed properly, you’ll get output that looks like this:
Creating 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:
When 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:
Save 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:
Check your configuration for errors:
If 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:
You’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:
Then create a simple function to send as an HTTP response to your browser. Use nano once again:
Open the file and enter the following code:
Then save and close the file. Afterwards, start Gunicorn and open the test module:
The output should look more or less like this:
That’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”.