As we already discussed to setup the Nginx server and creating virtual hosts on it. In this article, we will see about proxy server, reverse proxy server, and what they do and finally we discuss how we can configure Nginx as a reverse proxy server.
What is a proxy server?
A proxy server is an intermediary server that separates the end-user from the webserver. It acts as a gateway between the clients and the internet. If you are using a proxy server, the web request made by you will first go to the proxy server and then proxy server makes the request on your behalf, and collect the response from the webserver and forward it back to you. This forwarded data is displayed in the web browser. In this process, the proxy server hides the real identity and origin of a client. It also changes IP and encrypts the data while sending a request to the webserver. The benefits of using a proxy server are improved security, privacy benefits, access to blocked resources. The examples of a proxy server are squid proxy, Nginx, etc.
What is a reverse proxy?
A reverse proxy is a type of proxy server that retrieves the resources from one or more than one server and then returns it to the client. It appears as if they originated from the proxy server itself. It also works as an intermediary for the associated servers and clients. You can say a proxy server act on the behalf of the client(s) while a reverse proxy acts on the behalf of the server(s).
Some Uses of reverse proxy?
- It hides the real identity and characteristics of the origin server
- Act as a load balancer that means It can distribute the incoming request to several servers
- The reverse proxy server can be used to reduce the load of its origin server by caching the static and dynamic contents. This is known as ‘web acceleration’.
- It can optimize the content by compressing it that will reduce the loading time
- A reverse proxy can provide basic HTTP access authentication to a web server that doesn’t have any authentication
How to Configure Nginx as a Reverse Proxy?
As we understood the proxy server, reverse proxy server, and their working. Now we will go through a simple example to configure Nginx reverse proxy. I will use apache as a backend server because it is good at handling dynamic content while Nginx is good at handling static content in this way we can increase the efficiency of the whole system. Now we assume that apache is already setup as your backend server now follow the steps given below –
1. Install Nginx by using –
First, update OS repository –
sudo apt-get update
then install it by using –
sudo apt-get install nginx -y
2. Disable the default virtual host by using the following command-
unlink /etc/nginx/sites-enabled/default
3. Create a file with the name reverse-proxy.conf
we will create this file within the /etc/nginx/sites-available/
. Enter the following command in your terminal and press the enter
sudo nano reverse-proxy.conf
4. Now copy and paste the following information in reverse-proxy.conf
server { listen 80; location / { proxy_pass http://IP_Of_Backend_Server; } }
In the above config file, the important part is proxy_pass that allowing the request coming through the Nginx reverse proxy to pass along to IP_Of_Backend_Server
which is apache remote socket. Thus both the server shares the content.
Once completed, save the file and exit from the editor.
5. Activate the directive by linking to /etc/sites-enabled/
by using the following command –
sudo ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
6. Now test the Nginx reverse proxy –
check the Nginx configuration test and restart the Nginx to check its performance by using –
service nginx configtest
service nginx restart