How to make Nginx Server Listen on Multiple Ports

Posted on July 29th, 2019

Nginx is one of the best web server software in the market. We can install it with a single command on many of the Linux based operating systems. It does not come with lots and lots of modules like Apache, which means that it can provide better performance than Apache. Nginx is a multi-purpose piece of software that we can use as a web server or as a reverse proxy for another web server like Apache. In this short tutorial, we will discuss how to make Nginx Server listen on multiple ports.

In case you are hosting your website on your server and want it to listen on the custom port, it might not be a good idea. It is because we have to pass the port number along with the domain name in the browser and it is not convenient for users. However, if you are hosting the API on your server, it might be a good security feature to use a custom port.

If you are using Apache, you can follow our guide to change Apache ports on Ubuntu. But if you are using Nginx, It will be a lot easier to achieve this goal than Apache.

Finally, we can get started with the actual tutorial. I have installed Nginx on my server and I am considering that you too have Nginx already installed on your server. If not, just execute the following commands to install Nginx.

$ sudo apt-get update
$ sudo apt-get install nginx -y

Once the Nginx server is ready, we can start with the actual tutorial.

Make Nginx Listen on Multiple Ports

Like Apache, Nginx does not have a dedicated configuration file that contains port information. It means that we cannot edit a single file to apply changes globally. So, if your server has multiple virtual host files, you have to update all the virtual host files to apply the changes globally.

In this tutorial, we will update the default Nginx virtual host file to demonstrate how it works. Then, you can update the other virtual host files available on your server to apply the changes globally.

The virtual host files in Nginx are available in the /etc/nginx/sites-available directory. The name of the default Nginx virtual host file is, well, default. To edit the default virtual host in Nginx, execute the following command.

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

In the virtual host file, find a line containing listen. The listen directive will be followed by the port on which you want Nginx to listen. Just like the following example.

server {
    listen 80;

By default, it will be 80. You can change it to the desired port. In this case, I will replace it with 8080 and the updated configuration file should look like the following.

server {
    listen 8080;

Once you have edited the virtual host file, press CTRL+X followed by Y followed by the Enter key to save the file. Now, we have to restart the Nginx server to apply the changes. To restart the Nginx server, execute the following command.

$ sudo service nginx restart

Now, you will be able to access your server on the new port. However, it will not work on the default port anymore. If you are using the port we have used in this tutorial, use the following URL to access the site.


Do not forget to replace the placeholder with the domain name or the server IP. If you can access your website or application on this URL, you have successfully updated the listen port for Nginx.

To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It’s that easy!


Conclusion: It is very easy to make Nginx listen on multiple ports. It is surely easy than Apache. If you have multiple virtual hosts listening on multiple ports, execute the netstat -tulpn | grep nginx to get a list of ports that Nginx is already using on the server. So, this is how you can make Nginx listen on multiple ports. Let us know if you are facing any issue following the tutorial in the comment section given below! We will respond

6 Responses to “How to make Nginx Server Listen on Multiple Ports”

  1. Leo says:

    Hi! Great work on the article!
    Just one note: you should always check the syntax of your updated config file before restarting the nginx by usigng `sudo nginx -t`

  2. David says:

    “How to make Nginx server listen in multiple ports” is not the same as “Change Nginx listening port”
    This is not “multiple ports”. This is “different port”

  3. Mark says:

    This does NOT answer the question!

    • Bibin says:

      Please contact our support via live chat or ticket system, so we can better assist you directly with any service you have ordered through us.

Leave a Reply