In this tutorial, we will learn how to setup Nginx Reverse Proxy. but first, let's discuss in brief what a Reverse Proxy is & why do we need it?

Reverse Proxy

A reverse proxy is a server that takes the requests (HTTP/HTTPS) & then transfers or distributes them to the backend server. The backend server can be an application server like Tomcat, wildfly or Jenkins, etc or it can even be another web server like Apache.

But why do we even need a reverse proxy in front of the app or web server at all, we need it because,

1- It hides point of origin, thus making our backend server more secure & less susceptible to attacks,

2- Since the reverse proxy is the first point of contact for all requests, it can help encrypt/decrypt the request. This takes the load off from the backend server,

3- It can also be used for caching of content, which again reduces the load from other servers,

4- it can also act as a load-balancer.

We have already discussed how we can configure Apache Web Server as reverse proxy, now let’s talk about how we configure an Nginx reverse proxy.

Recommended Read : How to use Apache reverse proxy as Load Balancer


How to setup Nginx Reverse Proxy


Pre-requisites

We will need a backend server, it can be any app server or even a webserver. But remember, if you are using a web server that is also on the same server as Nginx reverse proxy, make sure that the other web server is not using the same TCP port as Nginx reverse proxy i.e. 80 & 443.

For the purpose of this tutorial, I will use a tomcat server hosted at a different server on IP 192.168.1.110 , working at port 8080 (refer to our tutorial here for detailed Apache Tomcat installation). As mentioned above, you can opt for a different application server or web server.

Also Read : Easy way to integrate Apache with modsecurity on Ubuntu


Installation


Now let’s discuss briefly, the installation of Nginx on,

Ubuntu

Nginx is available with default Ubuntu Repositories. So simply install it using the following command,

$ sudo apt-get update && sudo apt-get install nginx

CentOS/RHEL

We need to add some repos for installing Nginx on CentOS & we have created a detailed ARTICLE HERE for Nginx installation on CentOS/RHEL.

Now start the services & enable it for boot,

# systemctl start nginx

# systemctl enable nginx

At this point, we can open the web-browser & enter the server IP of Nginx, to see a default webpage & make sure the Nginx is working with no issues.


Configuration


Now that Nginx is installed & working we will move ahead with the Nginx reverse proxy configuration part. But first, we will remove the default configuration for the Nginx, it can be done with the following command,

# rm /etc/nginx/conf.d/default.conf

Alternatively, we can also remove the content inside the above-mentioned file & make the configuration for Nginx reverse proxy there, but I prefer to use a separate file for each site configured. So let’s create a new conf file for our Nginx reverse proxy,

# vi /etc/nginx/conf.d/test-proxy.conf

& make the following entries to the file,

server {

listen 80;

listen [::]:80;

server_name test-reverse-proxy.com;

location / {

proxy_pass ;

}

}

Now save the file & exit. Here in the configuration, we are telling the about the server_name & then under the ‘location’ section, we are providing the backend server i.e. our Apache tomcat server. Now to implement the changes made, we will restart the Nginx service but before that, we must check if the configuration made are correct or not,

# ngnix -t

or we can also provide the complete path for the configuration file,

# nginx -t -c /etc/nginx/conf.d/test-proxy.conf

Once the check returns with zero errors, we can restart the Nginx service,

# systemctl restart nginx

Note:- Also make sure that your backend server is working properly before moving onto the next step.


Testing


Now the next & final step is to check if the Nginx reverse proxy is working fine or not. So open a web browser & enter the Nginx server address/URL. Now when the page finishes loading, we should be seeing the apache tomcat page & not the default Nginx page, which we saw earlier.

That’s it, this was our tutorial on how to setup Nginx reverse proxy. We now end this tutorial, please feel free to send any questions or queries you have regarding this tutorial.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.