How to Change Nginx Port Number in Linux


How to Change Nginx Port Number in Linux

By default, Nginx listens to port 80 (http requests). For HTTPS requests, it listens to port 443, which is not enabled by default. However, if you want to make Nginx listen to a non-standard port number, you need to modify its config files. Let us see how to change Nginx port number in Linux.

 

How to Change Nginx Port Number in Linux

To begin with, open the Nginx config file in a text editor


$ vi /etc/nginx/sites-enabled/default [On Debian/Ubuntu]
$ vi /etc/nginx/nginx.conf [On CentOS/RHEL]

 

Next, change the port number as shown. Let’s say you want to make Nginx server listen to port 3200 instead the usual port 80. So we’ll look for the line beginning with listen directive and change its value from 80 to 3200.

 

[On Debian/Ubuntu]


listen 3200 default_server;
listen [::]:3200 default_server;

 

[On CentOS/RHEL]


listen 3200 default_server;

 

 

Next, you need to restart Nginx web server to bind it to the new port number.

You can verify local network socket tables using ss or netstat command. Port 3200 should be displayed in its output. Here’s the command


$ systemctl restart nginx
$ netstat -tlpn| grep nginx
$ ss -tlpn| grep nginx

 

In CentOS or RHEL systems, you need to install policycoreutils package and add the following rules to bind Nginx to port 3200


$ yum install policycoreutils
$ semanage port -a -t http_port_t -p tcp 3200
$ semanage port -m -t http_port_t -p tcp 3200

 

Restart Nginx to apply changes


$ systemctl restart nginx.service

 

Check the network tables


$ netstat -tlpn| grep nginx
$ ss -tlpn| grep nginx

 

You can also check if the port is accessible from other computers by opening a web browser and visiting http://your_domain.com:3200 or http://your_server_ip:3200

You will see the default Nginx web page.

Nginx Default Page

 

If you are unable to browse Nginx’s web page, make sure you have updated firewall rules to allow incoming requests from tcp port 3200.

 

That’s it! Now you know how to change Nginx port number in Linux. Nginx will start listening for incoming requests on new port number.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!