Introduction
There can be a situation where we want to setup multiple websites on our own infrastructure (can be on a single device, in case of static websites or perhaps different devices on their local network in case of some web-applications) and need to redirect the incoming URL request to the right application / device.
In such a situations, we can use a reverse-proxy to resolve the URLs and redirect ahead. We've many different options available for this - NGINX, traefik, Apache etc.
For this blog, we'll take the easist of them all - NGINX (and surely the most powerful and prominently used reverse-proxy).
Note:
Following Blog post is directed for the Linux users only. Windows and Mac users , please write in the comment section, if a similar blog post for Windows and Mac is also desired.
NGINX
Ngnix is a free and open-source tool written in C
to redirect the internet traffic to different devices / services. The source-code can be found here 1
Nginx is like a traffic cop for the internet. It's a web server that directs and manages the flow of data between the users and the websites they're trying to access. Imagine you're at a busy intersection, then Nginx is the traffic light making sure everything moves smoothly.
It handles tasks like serving web pages to users, managing incoming requests, and distributing those requests to the right places. Nginx is known for being fast and efficient, making websites load quickly and handle lots of visitors without slowing down.
It's not just for serving static web pages either; Nginx can also handle more advanced tasks like load balancing, SSL termination, and acting as a reverse proxy.
In simple terms, Nginx is a crucial piece of software that keeps the online traffic flowing smoothly and websites running efficiently.
Lets take a look at how we can benefit from this open-source and free tool to setup the redirection ourselves.
Installation
The installation of nginx is fairly straight forward. It offers installation of pre-built binaries (meaning package software) while also offering to compile the software package and install from the source-code ourselves.
NGINX Open Source is available in two versions:
- Mainline – Includes the latest features and bug fixes and is always up to date. It is reliable, but it may include some experimental modules, and it may also have some number of new bugs.
- Stable – Doesn’t include all of the latest features, but has critical bug fixes that are always backported to the mainline version. We recommend the stable version for production servers.
For the sake of brewity, we'll consider the installation method of Installing a Prebuilt Package.
Installing a Prebuilt Package
On CentOS/RHEL/ Oracle Linux/AlmaLinux/Rocky
If we happen to have Linux OS like CentOS or a RHEL etc
1. Install the EPEL repository:
sudo yum install epel-release
2. Update the repository:
sudo yum update
3. Install NGINX Open Source:
sudo yum install nginx
4. Verify the installation:
sudo nginx -v
On Ubuntu / Debian
If we happen to have a Debian based Linux OS like a Ubuntu, RaspberryPi OS or plain Debian etc
1. Update the Debian repository information:
sudo apt-get update
2. Install the NGINX Open Source package
sudo apt-get install nginx
3. Verify the installation:
sudo nginx -v
A detailed installation guide can be found here. 2
Once the installation is done, we can go ahead to setup NGINX in the reverse-proxy mode.
NGINX Configuration
Default Configuration:
- The main configuration file is usually located at
/etc/nginx/nginx.conf
. - The
default.conf
file, often found in/etc/nginx/conf.d/
or/etc/nginx/sites-available/
, contains configuration for the default server.
Nginx can be used as a reverse proxy to redirect specific incoming URL requests to other devices on our LAN network.
We will discuss two separate cases here.
CASE - 1
Lets discuss the first case. Let's say we have a device with IP address 192.168.1.2
on our LAN, and we want to redirect requests to http://somedomain.com/special
to this device.
In that case, all we've to do is create a new configuration file, let's call it special.conf
, in the Nginx configuration directory (e.g., /etc/nginx/conf.d/
) and add the following piece of code to the file
server {
listen 80;
server_name somedomain.com;
location /special {
proxy_pass http://192.168.1.2;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Other configurations for the rest of the site...
}
This configuration tells Nginx that when someone accesses http://somedomain.com/special
, it should forward the request to http://192.168.1.2
.
We must also test the configuration to make sure there are no syntax errors. To do so, we can simply run the following command in the terminal
sudo nginx -t
If everything is okay, reload Nginx to apply the changes using the following command
sudo service nginx reload
Now, when someone accesses http://somedomain.com/special
, Nginx will act as a reverse proxy and forward the request to the device at 192.168.1.2
on our LAN.
CASE - 2
Lets discuss the second case now. Let's say we have two separate devices on our LAN with IP addresses 192.168.1.2
and 192.168.1.3
, (these devices could be two separate Raspberry pi's running on our local Network, or perhaps two separate servers in a business network), and we want to redirect requests to http://sub1.somedomain.com
to 192.168.1.2
and requests to http://sub2.somedomain.com
to 192.168.1.3
.
In that case, all we've to do is create two configuration files, let's call them sub1.conf
and sub2.conf
, in the Nginx configuration directory (e.g., /etc/nginx/conf.d/
).
sub1.conf:
server {
listen 80;
server_name sub1.somedomain.com;
location / {
proxy_pass http://192.168.1.2;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Other configurations for the rest of the site...
}
sub2.conf:
server {
listen 80;
server_name sub2.somedomain.com;
location / {
proxy_pass http://192.168.1.3;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Other configurations for the rest of the site...
}
We must also test the configurations to make sure there are no syntax errors. To do so, we can simply run the following command in the terminal
sudo nginx -t
If everything is okay, reload Nginx to apply the changes using the following command
sudo service nginx reload
Now, when someone accesses http://sub1.somedomain.com
, Nginx will forward the request to 192.168.1.2
, and for http://sub2.somedomain.com
, it will forward to 192.168.1.3
.
Conclusion
So we can use the NGINX as the reverse-proxy which can help in redirecting the incoming URL requests to either multiple services or to multiple devices on the same network. Its not difficult at all, rather very easy and straight forward.
If you have any questions, opinions or feedback to give, please feel free to write in the comments below.
References :
For detailed info, please refer to the links cited below: