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

Reverse Proxy with Nginx Setup Guide

This step-by-step tutorial is going to 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 of 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 and works by handling all incoming client requests and distributing them to the back-end web, database, or other servers. Then it forwards the response back to the client.

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, which improve the 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 having to contact 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 suites deployed across the internet. It can works as a reverse proxy server by directing web traffic to specific servers.

Let me show you how to install Nginx on a variety of Linux distributions.

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 installed correctly by checking the software version. Enter the following:

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

2. Configuring Nginx

Let’s say 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 different server with IP address 172.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 blog.example.com subdomain, so that all requests are forwarded to 172.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/default

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;
        }
}

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 successful

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 172.0.1.10 that hosts 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 and it 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. Of course there are a plethora of ways to install and configure it which completely depend upon your requirement.

If this guide has helped you, please consider buying us a coffee.

Buy me a coffee!

Your support and encouragement are greatly appreciated!

Leave a Reply

Your email address will not be published.

Latest from Tutorials