Nginx Reverse Proxy

How To Set up Nginx as a Reverse Proxy

Spread the love

Last Updated on October 21, 2022

Nginx is a high-performance web server software. It can be used as a standalone web server, a load balancer, or a reverse proxy. In this article, I will show you how to use Nginx as a reverse proxy step-by-step.

What is a reverse proxy?

A proxy server is a go‑between or intermediary server that sends requests on behalf of clients (such as browsers) and web servers.

A simple explanation of a proxy is an example of a CEO and employees in an organization. For an employee to communicate with the CEO, they would need to pass through a Secretary or Personal Assistant and set up an appointment. In this case, the Secretary acts as a proxy to the CEO by handling all the enquiries and assigning appointments to the CEO.

A reverse proxy is a proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing to originate from the reverse proxy server itself.

The same case happens with computers, you can have a server sit in between the client and an application server and this server will handle all the requests and forward the necessary requests to the correct application server.

nginx reverse proxy
Reverse Proxy

Why use a reverse proxy?

There are several reasons you might want to set up a reverse proxy. Perhaps you want to access internal web applications from the internet without exposing these applications directly to the world. Or maybe you want to balance traffic across multiple backend servers or improve performance with caching. Reverse proxies can also prevent attackers from directly targeting your web servers, and they can help you comply with data privacy regulations by masking the true origin of your data.

Whatever your reasons, setting up a reverse proxy with Ngx_http_proxy_module for Nginx is easy. You just need to make a few configuration changes and set up some DNS records, and you’ll be up and running in no time!

How Nginx Reverse Proxy Works

Nginx reverse proxy is designed as an application-specific proxy. When it is set up, it will forward all requests to the next server. This is done to help speed up the website as well as to make it more secure. It also stores the request data and routes it to the next server according to the rules defined in the configuration file.

The advantages of using Nginx as a reverse proxy

  • Nginx Reverse Proxy improves performance by handling static content and caching them allowing for application servers to perform more tedious tasks.
  • Reverse Proxies protect the identity of Application servers thereby increasing the security of your stack as the application servers are not exposed to the whole internet. Using a Reverse Proxy set-up can allow your application servers to be situated in a private network making it even more secure for your application servers.
  • Using Nginx as a reverse proxy can allow us to use other Web servers for our application servers that are specialised for their task. This means that we can have one Application server using Apache as its web server and another application server using Caddy as its web server.

How to Set Up an Nginx Reverse Proxy?

Nginx can be used for numerous things, including setting it up as a reverse proxy. In this section, we will cover how to configure Nginx as a reverse proxy for your stack in ubuntu.

Prerequisites

  • An Ubuntu Server- You can get one on Vultr
  • A domain name – You can get one at Namecheap

Installing Nginx

Nginx can be installed with the apt package manager. Update the package index and install the Nginx package:

sudo apt update
sudo apt install nginx

The service will start automatically. To check whether Nginx is running, type:

sudo systemctl status nginx 

If Nginx is running, you should see something like this:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-10-12 14:07:23 BST; 1 week 1 day ago
       Docs: man:nginx(8)
    Process: 1269286 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 1269288 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 1269289 (nginx)
      Tasks: 5 (limit: 9459)
     Memory: 40.2M
        CPU: 4min 14.585s
     CGroup: /system.slice/nginx.service
             ├─1269289 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─1546704 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─1546705 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ├─1546706 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─1546707 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Notice: journal has been rotated since unit was started, output may be incomplete.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
lines 1-18/18 (END)

Configuring Nginx

Once Nginx has been installed, we can configure it as a reverse proxy.

First, you will need to edit the Nginx configuration file.

You can find this file at /etc/nginx/sites-available/default.

sudo nano /etc/nginx/sites-available/default 

Add the following lines to the file:

server { 
 listen 80;
 server_name example.com;
 
 root /var/www/html;
 
 index index.php index.html index.htm index.nginx-debian.html;

 location / { 
      proxy_set_header Host $host; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://102.x.x.x;
 }
}

The Nginx reverse proxy configuration contains a server block where we can instruct Nginx to listen to all requests through port 80(HTTP). We also specify the server name which is the domain name through which our application will be accessed.

The proxy_set_header directive instructs Nginx to pass headers to handle proxied requests

The proxy_pass directive instructs Nginx to pass all the requests to the specified destination server which in this case is http://102.x.x.x.

We can add as many proxied servers as we want by adding additional location blocks

server { 
 listen 80;
 server_name example.com;

 root /var/www/html;
 
 index index.php index.html index.htm index.nginx-debian.html;

 location / { 
      proxy_set_header Host $host; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://102.x.x.x;
 }
 
 location /blog { 
      proxy_set_header Host $host; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://104.x.x.x;
 }
}

In this scenario, we are passing all requests destined for /blog to another application server that may contain a WordPress CMS or a Webflow CMS installation.

Therefore, when a user navigates to example.com/blog, their request is proxied to the application server with the IP address 104.x.x.x by Nginx.

Test and Restart Nginx

The last step is to ensure that the Nginx configurations are correct.

sudo nginx -t 

If the configurations are okay, we can now restart the Nginx service

sudo service nginx restart

FAQs

What is the difference between a forward proxy and a reverse proxy?

A forward proxy is set up to sit in front of the clients and intercept their requests and then pass it to the server. Its sole purpose is to protect the identity of the clients. The application server in this case will not know which client made the request as it only knows of the forward proxy.

A reverse proxy, on the other hand, sits in front of the application servers and protects the identity of the servers. A client will only know of the reverse proxy but won’t know of the application servers behind the reverse proxy.
nginx reverse proxy

Is reverse proxy the same as a load balancer?

No. A reverse proxy accepts requests from a client and forwards them to an application server and then returns the response back to the client.

A load balancer, on the other, hand distributes incoming client requests among a group of servers.

In the case of a reverse proxy, the application servers can be set up to perform different tasks i.e one server is sending out emails, another server is handling payments etc. while for load balancing, all the application servers should be performing the same tasks, for example, an e-commerce backend API can be deployed to multiple instances and have a load balancer distribute the requests between them.

You can learn how to configure Nginx as a load balancer here.

Is Nginx forward or Reverse Proxy?

Nginx is a reverse proxy server, which means it can be used as a gateway between a web server and a client. It can sit in front of your application servers and intercept client requests and forward them to the relevant application server.

Conclusion

Nginx is a powerful tool that can be used as a web server, a load balancer, a reverse proxy, and an HTTP cache. This guide showed you how to set up Nginx as a reverse proxy. I hope you learnt a lot and if you enjoyed this article, you can consider reading this article on how to set up Nginx as a load balancer or this article on how Nginx compares to Apache Web server

Thank you for reading.

Similar Posts

Leave a Reply

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