How To Set up Nginx Server as a Reverse Proxy
In this tutorial, I will show you how to set up a reverse proxy in Nginx. You know that Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. In this example, I will show you step by step that how we can set up Nginx as a reverse proxy.
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 example of a proxy server is an example of a CEO and employees in an organization. Let's assume, for an employee to communicate with the CEO, they would need to pass through a Secretary or Personal Assistant and set up a meeting. In this case, the Secretary acts as a proxy to the CEO by handling all the inquiries and assigning appointments to the CEO.
So we can say for a proxy server like reverse proxy is a proxy server that retrieves resources on behalf of a client from one or more servers.
How to set up an Nginx reverse proxy?
Nginx may be used for many things, including setting it up as a reverse proxy. In this tutorial, I will cover how to configure Nginx as a reverse proxy for your stack in ubuntu. Nginx can be installed with the apt package manager in ubuntu. 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 successfully, 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 need to configure it as a reverse proxy. First, we will need to edit the Nginx configuration file. We 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;
}
}
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;
}
}
Now restart Nginx with the following command and test:
sudo service nginx restart
Read also: Dockerizing Laravel Application with Nginx and MySQL
Hope it can help you.