# 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](https://www.ionos.ca/digitalguide/server/configuration/ubuntu-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](https://www.ionos.ca/digitalguide/server/know-how/ubuntu-the-linux-system-for-everyone/) 22.04.

Tip We also have instructions for [installing NGINX on Ubuntu 20.04](https://www.ionos.ca/digitalguide/server/configuration/install-nginx-on-ubuntu-2004/). And you can read more about what a [proxy server](https://www.ionos.ca/digitalguide/server/know-how/what-is-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:

```bash
$ sudo apt update
$ sudo apt install nginx
```

Confirm with \[Y\] and press \[Enter\] to apply the settings.

Next, configure your [firewall](https://www.ionos.ca/digitalguide/server/security/what-is-a-firewall/) so that NGINX has access to your server. You can use the following command to add an exception:

```bash
$ sudo ufw allow 'Nginx HTTP'
```

Then check that the installation was successful:

```bash
$ systemctl status nginx
```

If NGINX was installed properly, you’ll get output that looks like this:

```bash
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"
```

### 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:

```bash
$ sudo nano /etc/nginx/sites-available/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:

```txt
server {
    listen 80;
    listen [::]:80;
    server_name your_domain www.your_domain;
        
    location / {
        proxy_pass http://server_address;
        include /etc/nginx/proxy_params;
    }
}
```

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:

```bash
$ sudo ln -s /etc/nginx/sites-available/your_domain/etc/nginx/sites-enabled/
```

Check your configuration for errors:

```bash
$ sudo nginx -t
```

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:

```bash
$ sudo systemctl restart nginx
```

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:

```bash
$ sudo apt update
$ sudo apt install gunicorn
```

Then create a simple function to send as an HTTP response to your browser. Use nano once again:

```bash
$ nano test.py
```

Open the file and enter the following code:

```txt
def app(environ, start_response):
	start_response("200 OK", [])
	return iter([b"This is a test"])
```

Then save and close the file. Afterwards, start Gunicorn and open the test module:

```bash
$ gunicorn --worker=2 test:app
```

The output should look more or less like this:

```bash
[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 (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
```

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”.


This is a markdown version of: [https://www.ionos.ca/digitalguide/server/configuration/nginx-reverse-proxy/](https://www.ionos.ca/digitalguide/server/configuration/nginx-reverse-proxy/) for AI/LLM consumption.