Simple way to configure Nginx Reverse Proxy

A reverse proxy is a server that takes the requests (http/https) & then transfers or distributes them to backend server. 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 app or web server at all, we need it cause,

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

2- Since reverse proxy is first point of contact for all requests, it can help encrypt/decrypt the request. This takes the load off from 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 a Nginx reverse proxy.

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

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 same tcp port as nginx reverse proxy i.e. 80 & 443.

For the purpose of this tutorial, I will using 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 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 simple 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 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 http://192.168.1.110:8080/;

}

}

Now save file & exit. Here in the configuration, we are telling the about the server_name & than under ‘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 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 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, our nginx reverse proxy is now ready & working fine. 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 | Google Plus

Donate us some of your hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

1 Response

  1. Elmeknassi says:

    Thank you , never thought that was this easy

Leave a Reply

Your email address will not be published. Required fields are marked *