Reverse Proxy with Nginx: A Step-by-Step Setup Guide

This step-by-step tutorial will show you how you can easily set up a reverse proxy with Nginx to improve security and performance.

One of the more popular reverse proxy tools at the moment is Nginx. Although Nginx itself is a web server, it does an outstanding job serving as a reverse proxy.

What is a Reverse Proxy?

A reverse proxy is a server that sits between internal applications and external clients, forwarding client requests to the appropriate server.

The reverse proxy service acts as a front-end, handles all incoming client requests, and distributes them to the back-end web, database, or other servers.

Using Nginx as a reverse proxy gives you several additional benefits:

  • Load Balancing – Nginx can perform load balancing to distribute clients’ requests across proxied servers, improving performance, scalability, and reliability.
  • Improved Security – With a reverse proxy, clients will not have information about our backend servers, so there is no way any malicious client cannot access them directly to exploit any vulnerabilities. From the clientโ€™s point of view, this makes it look as though everything is coming from one place.
  • Better Performace – With Nginx as a reverse proxy, you can cache the pre-rendered versions of pages to speed up page load times. It works by caching the content received from the proxied servers’ responses and using it to respond to clients without contacting the proxied server for the same content every time.
  • SSL Termination – Nginx can act as an SSL endpoint for connections with the clients. It will handle and decrypt incoming SSL connections and encrypt the proxied serverโ€™s responses.
How Nginx Reverse Proxy Works

How to Set Up a Reverse Proxy with Nginx

1. Installing Nginx

Nginx is the most popular web server suite deployed across the internet. It can work as a reverse proxy server by directing web traffic to specific servers.

Let me show you how to install Nginx on various Linux distributions.

Related: How to Install and Configure Nginx Web Server

To install Nginx on DebianUbuntuLinux Mint, Kali, and other Debian or Ubuntu derivatives, run the following commands:

sudo apt update
sudo apt install nginx

On FedoraCentOS, Red Hat, and other Fedora or Red Hat derivatives, run the following commands:

sudo dnf upgrade
sudo dnf install nginx

To install Nginx on Arch LinuxManjaro, and other Arch derivatives, run the following commands:

sudo pacman -Syu
sudo pacman -S nginx

After installation is completed, you can verify that Nginx is installed correctly by checking the software version. Enter the following:

nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

2. Configuring Nginx

We’ve installed the primary site at example.com domain name on the same server that runs our Nginx. However, our WordPress blog site is installed at blog.example.com subdomain on a different server with IP address 127.0.1.10.

Related: How to Configure Nginx to Work with PHP via PHP-FPM

We need to configure a reverse proxy with Nginx to proxying requests for the blog.example.com subdomain so that all requests are forwarded to 127.0.1.10. To do that, follow the steps shown below.

1. Disable the default pre-configured Nginx virtual host:

unlink /etc/nginx/sites-enabled/defaultCode language: JavaScript (javascript)

2. Enter the directory /etc/nginx/sites-available and create a reverse proxy configuration file.

cd /etc/nginx/sites-available
sudo vim example.conf

Paste the following Nginx configuration.

server {
        listen 80;
        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }
}

server {
        listen 80;
        server_name blog.example.com;

        location / {
                proxy_pass http://127.0.1.10:80;
        }
}Code language: PHP (php)

As you can see,  you could continue running example.com on the same Nginx instance, but Nginx will redirect all incoming connections for blog.example.com on port 80 to the 127.0.1.10 server, listening on port 80.

3. Save the file and then activate it by creating a symlink:

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/

4. Test Nginx for any configuration errors:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulCode language: HTTP (http)

5. If there are no errors, restart Nginx for the changes to take effect:

sudo systemctl restart nginx

Launch a web browser and go to your WordPress blog URL. You will be proxied through Nginx to 127.0.1.10, hosting your WordPress blog.

Congratulations, youโ€™ve successfully set up Nginx to work as a reverse proxy. Now you can direct traffic that is supposed to go to the target server to the reverse proxy server, which will wind up at the correct destination.

You can learn more about all the reverse proxy directives in Nginxโ€™s detailed index of directives.

Conclusion

Now you know how to set up a reverse proxy with Nginx. As you can see, the Nginx reverse proxy configuration is a simple process. But, of course, there are many ways to install and configure it, which depend entirely upon your requirement.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%