Press Ctrl/Cmd + P to print
or save as PDF

Set Up Reverse Proxy in Linux Based VPS with Nginx in 4 Simple Steps

Nginx is an open-source HTTP web server and also one of the most well-known web servers for its performance. By using Nginx in your VPS, you have the ability to redirect the URL of a website to another address. In a Linux based VPS, a reverse proxy serves as a medium between the client and the server by receiving client requests and forwarding them to other servers, and then it returns the server’s response back to the client. In this article, we will guide you on setting up a nginx reverse proxy on your VPS. The Linux distribution used here is the Ubuntu version 18.04.

Before starting the setup, you will first need to access your VPS with SSH and have an Apache web server installed. Configure Nginx to the front of Apache so that it will handle all static content whereas Apache will handle the dynamic content. Then define the IP address of Nginx Proxy Server as 192.x.x.1 while Apache server as 192.x.x.2 as a back-end server.

Step 1: Install Nginx

To install Nginx onto your Ubuntu, first, open your terminal by searching the application or right-clicking on the desktop and select the “Open Terminal” option. Then, use the following command to help you update your repositories and install nginx line by line.

sudo apt-get update
sudo apt-get install nginx

Step 2: Disable Default Virtual Host

After the installation for Nginx is completed, disable the virtual host by using the following command in your terminal.

sudo unlink /etc/nginx/site-enabled/default

Step 3: Create Reverse Proxy

Create a configuration file named “reverse-proxy.conf” within the directory “etc/nginx/sites-available” to keep the reverse proxy information after disabling the default virtual host. To access the directory, use the cd option such as the following command.

cd etc/nginx/sites-available/

Then create and open the configuration file with the vi file text editor using the following command.

vi reverse-proxy.conf

Insert the following text into the configuration file.

server {
    listen 80;
    location / {
        proxy_pass http://192.x.x.2;
    }
}

This allows both Nginx and Apache to share content by allowing requests to go through the Nginx reverse proxy to pass along Apache’s remote socket. Make save changes to it and exit vi text editor by entering “:wq”. Use “ngx_http_proxy_module” in your terminal if you wish to pass information to other servers.

To activate the directives, use the following command to link to the directory “/sites-enabled/”.

sudo ln -s /etc/nginx/sites-available/reverser-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf

Step 4: Testing

Finally, we will need to run a configuration test and restart it to check its performance. To do the configuration test, use the following command.

service nginx configtest

To restart Nginx, use the following command.

service nginx restart

Your Nginx reverse proxy will then be completed. In the case where you have a failed test, there is a high chance that your Apache isn’t set up properly.