How to Change Nginx Port in Linux

Nginx is an open source stable server that powers some of the most high traffic websites in internet today. Among web services, Nginx web server can be successfully deployed as an load-balancer, web reverse proxy or as a POP and IMAP proxy server.

By default, Nginx HTTP server listens for incoming connection and binds on port 80, which represents the standard web port. However, the TLS configuration, which is not enabled by default in Nginx, listens for secure connections on port 443.

In order to make Nginx HTTP server to listen for incoming web connections on other non-standard ports, we need to edit the main configuration file and change or add a new statement to reflect this fact.

In Ubuntu and Debian based system, we need to modify the /etc/nginx/sites-enabled/default file and on RHEL and CentOS based distributions edit /etc/nginx/nginx.conf file.

To begin with, open Nginx configuration file with a text editor, and change the port number as shown in the below excerpt.

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

In this excerpt we’ll configure Nginx HTTP server to listen for incoming connections on port 3200. Search for the line that begins with listen statement in server directive and change the port from 80 to 3200, as illustrated in the below image.

listen 3200 default_server;
Change Nginx Port in Ubuntu

Change Nginx Port in Ubuntu

Change Nginx Port in CentOS

Change Nginx Port in CentOS

After altering Nginx port statement, you need to restart the web server in order to bind on the new port on Debian based Linux distributions. Verify local network sockets table with netstat or ss command. Port 3200 should be displayed in your server local network table.

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

In CentOS or RHEL based Linux distribution you need to install policycoreutils package and add the below rules required by SELinux for Nginx to bind on the new port.

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

Finally restart Nginx HTTP server to apply changes.

# systemctl restart nginx.service 

Check network tables listening sockets.

# netstat -tlpn| grep nginx
# ss -tlpn| grep nginx
Verify Nginx New Port

Verify Nginx New Port

To check if the web server can be accessed form computers in your network, open a browser and navigate to your server IP address or domain name on port 3200. You should see Nginx default web page, as illustrated in the below screenshot.

Nginx Default Page

Nginx Default Page

However, if you can’t browse Nginx web page, return to server console and check the firewall rules to allow incoming traffic on port 3200/tcp.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Matei Cezar

I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

3 Responses

  1. KANBER CIRIT says:

    Thank you very much for this document you have prepared.

  2. Bile Bakshi says:

    Lots of Thanks for this article. I was completely blind after getting lots of issues in nginx, but with the help of your tips and tricks of nginx. it is working properly. Thank you……

  3. TheHighTechRedneck says:

    Don’t forget that on RedHat or CentOS you may need to change the SELinux port context for Nginx to use the new port

Got something to say? Join the discussion.

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

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