How to Change NGINX Port Number in Ubuntu

Last updated on October 6th, 2025 at 04:58 am

NGINX is a popular and powerful web server capable of handling high traffic and extreme server loads. By default, NGINX server runs on port 80. But sometimes system administrators may need to change NGINX port number for various reasons. In this article, we will learn how to change NGINX port number. Although we will cover these steps for Ubuntu, they can be used for any Linux system.

Why Change Port Number

There are several reasons why you may need to change port number of NGINX server.

1. Improved Security – By default, NGINX runs on port 80. Most hackers and malicious bots target common ports like 80 for their attacks. By configuring NGINX to run on a non-standard port, you will make it difficult for automated scripts and bots to hack your server.
2. Run Multiple Services – If you are running other services such as Apache that also use port 80, then you may need to change NGINX port number.
3. For Testing Purposes – If you are running NGINX in a development or staging environment, then you can run it on a different port to avoid interfering with production server, that runs on port 80. This will allow you to run multiple NGINX instances on a single server.
4. Specific Application Requirements – Sometimes, you may need to run NGINX on a different port, simply to integrate with other systems. They may already be configured to read input from or send output to another port. So NGINX will need to listen to that port.
5. Firewall Rules – Many organizations block port 80 by default, for security purposes. They know that port 80 is a common target of online attacks and it is better to avoid using it completely. In such cases, you will need to change NGINX port number to something else.

How to Change NGINX Port Number in Ubuntu

Now let us look at the steps to change NGINX port Number in Ubuntu.

1. Open NGINX Configuration File

The first step is to open NGINX configuration file in a text editor. It is typically located at /etc/nginx/nginx.conf. If you have site specific configuration file, then you will find it at /etc/nginx/sites-available.

$ sudo vi /etc/nginx/nginx.conf

2. Change Port Number

In the configuration file, look for listen directive in the server block. If you have multiple server blocks, then look for the required server block.

Change the following line, as shown, to change port number from 80 to 8000.

listen 80;

to

listen 8000;

Similarly, if you want to change HTTPS port from 443 to 8443, then modify the following rule.

listen 443 ssl;

to

listen 8443 ssl;

3. Restart NGINX Server

Save and close the file. Test NGINX configuration.

$ sudo nginx -t

Restart NGINX server to apply changes.

$ sudo systemctl restart nginx
OR
$ sudo service nginx restart

4. Verify New Port

You can verify that NGINX is running on new port, using ss or netstat tool.

$ sudo ss -antpl | grep nginx
OR
$ sudo netstat -tlpn | grep nginx

5. Modify Upstream Services

Generally, NGINX is used in combination another upstream service such as Apache web server. In such cases, you need to modify Apache configuration to communicate with NGINX on new port number.

6. Modify Downstream Services

Generally, most incoming requests are parsed by firewall before being passed to NGINX server. So you will have to also modify downstream firewall rules by opening the new port and closing the old port.

Conclusion

In this article, we have learnt how to change port number in NGINX. If you are running a high-traffic website, it is advisable to change the default port of NGINX server for security purposes. This will keep malicious hackers and bots from targeting your site for common vulnerabilities.

Also read:
How to Enable CORS in NGINX
How to Increase Request Timeout in NGINX
How to Uninstall NGINX Server

Leave a Reply

Your email address will not be published. Required fields are marked *