NGINX is an open-source reverse proxying software. NGINX has various other peculiarities, including maximum performance, simplicity, high stability, etc.
Let’s see how to host multiple domains in NGINX;
First, we need to create two server blocks, one for each domain, as shown below. Here, our first domain is named example1.com, and the second one is example2.com. We need to specify the directory from which the content is passing and the file type. For that purpose, we need to add that details in our configuration like below.
server {
listen 80;
root /var/www/html/example1.com;
index index.html;
server_name example1.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /var/www/html/example2.com;
index index.html;
server_name example2.com;
location / {
try_files $uri $uri/ =404;
}
}
Restart the NGINX server to apply changes.
$ sudo nginx -t
$ sudo systemctl restart nginx
Next, Let’s see how to make Nginx Server Listen on Multiple Ports;
To add multiple ports, first, we need to make changes in the virtual host file.
The virtual host files in Nginx are available in the /etc/Nginx/sites-available directory. To edit the default virtual host in Nginx, execute the following command.
$ sudo nano /etc/nginx/sites-available/default
In our host file, there will be a “listen” directive that will specify the port to which the Nginx should listen.
server {
.
.
listen 80;
.
.
}
The value for listen will be 80 by default. But it’s possible to change it to the desired value. For instance, here we are updating it to 8080 and it will look as shown below.
server {
.
.
listen 8080;
.
.
}
After updating the listen, save the file. Now we have to restart the Ngnix service to apply these changes. To restart the Nginx service, follow the commands below.
$ sudo service nginx restart
Now, it is able to access your server on this newly updated port.
Now, you will be able to access your server on the new port.
http://DOMAIN_OR_SERVER_IP:8080
To make able to listen multiple ports by Nginx, we can add multiple listen directives and can add different ports for each listen directive.
server {
listen 80;
listen 8000;
server_name example.org;
root /var/www/;
}
In conclusion, this Nginx handles multiple domains and ports.