Configure Nginx as Reverse Proxy on Ubuntu

How to Configure Nginx as Reverse Proxy on Ubuntu Linux VPS

A reverse proxy is a server, software application, or cloud service that sits in front of one or more web servers to snoop on and examine incoming client requests before sending them to the web server and then relaying the server’s response to the client. How to Configure Nginx as Reverse Proxy on Ubuntu Linux VPS is what you will learn in this article.

To function effectively, the client and server regularly exchange information. A web server typically uses reverse proxies. A reverse proxy or gateway doesn’t require any additional setups and appears to the client just like a regular web server. The client submits routine queries, and the reverse proxy chooses where to forward the data, sending the finished product to the client as though it were the origin.

Prerequisites to Configure Nginx as Reverse Proxy on Ubuntu

To let this tutorial works correctly, provide the options below.

  • A system running Ubuntu.
  • A non-root user with sudo privileges.
  • A domain name pointed at your server’s public IP. This will be configured with Nginx to proxy your application server.

Tutorial Setup Nginx as Reverse Proxy on Ubuntu

Installing an Nginx reverse proxy on a Linux computer has several advantages. Performance can be efficiently increased, and virus protection can be improved. In a Linux terminal, configuring the Nginx reverse proxy is straightforward. Although there are numerous ways to install and customize it, they all depend entirely on your needs.

Previously, you have learned how to Install Nginx in Ubuntu. Let’s go through the steps of this article to review Configure Nginx as Reverse Proxy on Ubuntu.

Step 1. Install Nginx

As usual, we start with updating the repository index and then installing Nginx.

sudo apt update
sudo apt install nginx

To confirm the installation, press Y and hit ENTER to accept the defaults when you are asked to restart services. Then, use the command below to allow access to Nginx through your firewall.

sudo ufw allow 'Nginx HTTP'

To check if Nginx is running, type:

systemctl status nginx

Step 2. Configure Server Block and Create the Nginx Reverse Proxy

Now that a custom server block is added with your domain and app server proxy, it is time to configure it and create an Nginx reverse proxy. Instead of immediately changing the default configuration, it is advised that you develop a custom configuration file for your new server block additions.

Using nano or another text editor of your choice, make a new Nginx configuration file and open it:

sudo nano /etc/nginx/sites-available/your_domain

Then, add the following to your new file.

/etc/nginx/sites-available/your_domain

server {
    listen 80;
    listen [::]:80;

    server_name your_domain www.your_domain;
        
    location / {
        proxy_pass app_server_address;
        include proxy_params;
    }
}

Do not forget to replace your_domain and app_server_address with your own. Press CTRL+O and then CTRL+X to save and exit.

Create a link from this configuration file to the sites-enabled directory that Nginx reads when it starts up to enable it.

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Run the command below to test the configuration file to check any probable syntax errors.

sudo nginx -t

If you face no problem, apply your changes by restarting Nginx:

sudo systemctl restart nginx

If your application server is active, you can now access Nginx from a local browser by configuring it as a reverse proxy for your application server. You can move forward with starting your intended application server if you have one but have not yet started it.

Step 3. Test Nginx Reverse Proxy Configuration

To check the performance of Nginx, you can run an Nginx configuration test and restart Nginx. So, run:

service nginx configtest
service nginx restart

If you receive no error, you have set up your Nginx reverse proxy correctly, and you must view the “Hello World!” page.

Also, to configure the buffers, you can add the following lines in the file:

location / {

    proxy_pass http://localhost:3000/;

    proxy_buffering off;

}

Add the following lines to configure the request headers:

location / {

    proxy_pass http://localhost:3000/;

    proxy_set_header X-Real-IP $remote_addr;

}

FAQ

Conclusion

In this article, you learned How to Configure Nginx as Reverse Proxy on Ubuntu Linux VPS. Following the steps of this tutorial enables you to set up Nginx as a reverse proxy to provide access to your application servers, which are otherwise only locally available. A basic reverse proxy can be made with just the configurations we put in the proxy configuration file. You will need to add extra configurations to the file for complicated applications, though.

If you follow the above steps properly then you can smoothly install without any errors, but please do not hesitate to contact us if you encounter any problems. Our technical support team will try their best to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked.