How to Change Nginx Port on Ubuntu Linux

Computer monitor with security lock icon
Computer monitor with security lock icon

Nginx, probably the second most popular HTTP server in use today, is easy to manage and configure. Nginx is a lightweight and efficient HTTP server but can be served as a proxy server and more.

Sometimes you’ll want Nginx to listen and communicate over not its default port but a different port instead. The steps below can be a good starting point when you want to do that.

For example, if you want the Nginx HTTP server to sit behind a proxy server, then the proxy server must be configured to listen on the default port 80. In this case, Nginx must also be configured to listen on a different port than 80 since two services can’t be assigned one port to listen on. There would be contentions.

This tutorial will show students and new users how to quickly switch the Nginx default port from 80 to something else. Like 8082. in this way, other services can use port 80 to communicate.

When you’re ready to make this change, continue below:

Identifying Nginx Port Config File

To change the Nginx default port number, you need to look in a single directory, unlike Apache2. This is Nginx default virtual hosts directory /etc/nginx/sites-available. This directory is where you’ll find individual virtual host configuration files.

Each file contains a port for Nginx to listen and communicate. You’ll do it in each virtual host file if you want to change the Nginx port.

Below is the location to can change the Nginx default port numbers

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

Changing Nginx Port Number

Now that you’ve identified the files, you can change the Nginx port number and continue below to make those changes. For this post, we’ll change the default ports 80 to 8082 and 443 to 444.

First, run the commands below to open the port—conf file.

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

Then change the Listen line from 80 to 8082

server {
        listen 8082 default_server;
        listen [::]:8082 default_server;

        listen [::]:444 ssl;
        ssl_certificate /path_to_cert_file;
        ssl_certificate_key /path_to_cert_key_file; 

        root /var/www/html;
        index index.php index.html index.htm;

        server_name _;

        location / {
             try_files $uri $uri/ =404;

Restart Nginx

Now that you’ve changed the port number in all the files, run the commands below to restart Nginx.

sudo systemctl restart nginx.service

After that, the Nginx service will listen to the port you assigned above.

That’s it! This is how one changes Nginx port numbers.

You may also like the post below:

Posted by

I love computers; maybe way too much. What I learned I try to share at

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: