How to Create a Reverse Proxy in Nginx

How to Create a Reverse Proxy in Nginx

For those who are looking to understand how to create a reverse proxy in Nginx, here’s a definitive guide with all the information you need. 

There are several advantages to using a reverse proxy server. It can provide a single point of contact to clients for accessing services. It also serves as a cache server and can improve performance. The article briefly discusses the steps to configure a reverse proxy server in Nginx.

What is a reverse proxy?

In Linux, we can assume a reverse proxy as a link between the client and the server. The reverse proxy receives a request from the client and forwards it to the server. The server responds to the reverse proxy, which is disseminated to the client by the reverse proxy. It appears to the client that the proxy server has served the request. 

Web servers use reverse proxies. A reverse proxy appears to the client like a web server. You don’t need to make any special configurations. The client makes a web request to the reverse proxy. The proxy or the gateway can decide whether to forward this request to the web server. 

Most enterprises use a single reverse proxy to handle all incoming requests. Depending upon the requests, it can be forwarded to different backend servers such as Apache, Tomcat, Express, or NodeJs. 

Nginx reverse proxy server

Nginx proxy is the most widely used open-source server on the internet. The following are the major features of Nginx reverse proxy:

  • Using it, you just need a single IP address to access all backend services.
  • By serving the static content via nginx reverse proxy, the load on the backend servers is reduced.
  • It can navigate through the firewall. Hence servers behind the firewalls can be protected.
  • It can act as a cache or buffer. Hence the client can receive the response with minimal latency.
  • Utilizing a single access point, access control can be greatly simplified.

The following figure shows a typical nginx reverse proxy setup.

reverse proxy setup

Create a reverse proxy in Nginx – Setup

We will discuss how to set up an Nginx proxy in front of an Apache web server. The static contents will be served by the Nginx proxy. For the dynamic content, the apache web server is consulted. This will definitely improve the performance of serving the web content. Following are the steps to create a reverse proxy in Nginx and configure it:

  • Install Nginx.
  • Disable the virtual host.
  • Create the reverse-proxy.conf file.
  • Optionally set the header for nginx reverse proxy.

Install nginx

The very first step is the installation of Nginx. For this purpose, first, update the system with the help of the following command typed on your terminal:

$sudo apt-get update

You may want to upgrade your system as well with the help of the following command:

$sudo apt-get upgrade -y

Now, install the Nginx with the apt command as follows:

$sudo apt-get install nginx

Also, install the following packages:

$libnginx-mod-http-geoip2 nginx-common nginx-core nginx-proxy

Before moving forward, you can check if Nginx is active with the help of the following command:

$sudo systemctl status nginx

Disable the default virtual host

The next step is to disable the virtual host using the following command:

$sudo unlink /etc/nginx/sites-enabled/default

Create the reverse-proxy.conf file

Now, we will create a file reverse-proxy.conf in the folder etc/nginx/sites-available. This will keep the reverse proxy information. Move to the directory using the following command:

$cd etc/nginx/sites-available/

Using your favorite editor, open the file as follows:

$vi reverse-proxy.conf

Paste the following information in the file:

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

In the above lines, we have specified the proxy information as 192.x.x.2. The request will be received on port 80 and will be passed to the Apache web server running on 192.x.x.2. So, both the nginx and apache web server will share the content. Now, exit the vi editor by typing “:wq” and pressing enter.

Set nginx proxy header values

By default, nginx propagates its IP address to the backend server. If you want to forward the IP address of the host and port information, you can set the header values. For managing heavy workloads, nginx may buffer the data as well.  However, to reduce the latency, it is desirable to turn off buffering. To turn the buffering off and sets the header to forward client details, use reverse-proxy lines.conf file:

location / {
proxy_pass http://192.x.x.2;  proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;

Activate the directives

We will need to activate the directives. So, we will link the /sites-enabled/ using the following command:

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

Testing nginx and Nginx reverse-proxy

After we create a reverse proxy in Nginx, we will test it. We will run the nginx configuration test and restart nginx to check its performance. 

Check if nginx is running

First, we will check if Nginx is running. Type the following command on your Terminal:

$service nginx configtest

Restart nginx

Now, restart Nginx using the following command:

$service nginx restart

During the execution of the last two steps, if you are receiving a failure, then your Apache server has not been properly set up.

Browse the website

Now you can browse the website running, for example, If you see the test page, it means the Nginx proxy server has been properly configured.

In this article, we have seen how to create a reverse proxy in Nginx. We discussed the benefits of using a reverse proxy and the basic steps in configuring an nginx proxy for an apache webserver.

If this guide helped you, please share it. 

Related Posts