How to Change Nginx Listening Port on Linux: 3 Simple Methods
This guide walks you through verifying the Nginx process, checking its current listening port, editing the configuration file, and applying the change using systemctl restart, the nginx -s reload command, or a manual kill signal, with concrete command examples and expected outputs.
1. Verify Nginx is Running
Use systemctl status nginx to confirm the service is active. The output shows the master and worker processes and confirms the configuration file syntax is OK.
# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2024-08-09 17:26:42 CST; 4h 14min ago
Process: 1437 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 1439 (nginx)
CGroup: /system.slice/nginx.service
├─1439 nginx: master process /usr/sbin/nginx
├─1440 nginx: worker process
└─1441 nginx: worker process2. Check the Current Listening Port
Run netstat -tunlp | grep nginx to see which ports Nginx is bound to.
# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1439/nginx: master
tcp6 0 0 :::80 :::* LISTEN 1439/nginx: master3. Modify the Listening Port in nginx.conf
Edit the configuration file (usually /etc/nginx/nginx.conf) and change the listen directive. For example, change line 39 to listen 12111;, then save the file.
4. Apply the Change – Method 1: systemctl restart
Restart the service so the new port takes effect, then verify with netstat again.
# systemctl restart nginx
# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:12111 0.0.0.0:* LISTEN 2994/nginx: master
tcp6 0 0 :::80 :::* LISTEN 2994/nginx: master5. Apply the Change – Method 2: nginx -s reload
After editing the file, reload the configuration without a full restart.
# nginx -s reload
# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:12112 0.0.0.0:* LISTEN 2994/nginx: master
tcp6 0 0 :::80 :::* LISTEN 2994/nginx: masterThe -s flag sends a signal to the master process; reload corresponds to SIGHUP, causing Nginx to reread its configuration.
6. Apply the Change – Method 3: kill -1 (SIGHUP)
Find the master PID with ps -ef | grep nginx, then send SIGHUP manually.
# ps -ef | grep nginx
root 2994 1 0 21:48 ? 00:00:00 nginx: master /usr/sbin/nginx
# kill -1 2994
# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:12113 0.0.0.0:* LISTEN 2994/nginx: master
tcp6 0 0 :::80 :::* LISTEN 2994/nginx: masterCommon kill signals useful for Nginx management: kill -1 pid – reload configuration (SIGHUP) kill -15 pid – graceful shutdown (SIGTERM) kill -9 pid – force kill (SIGKILL)
7. Summary of Steps
To change the listening port, edit nginx.conf, then choose one of the three methods—restart, reload, or manual SIGHUP—to apply the change, verifying each time with netstat -tunlp | grep nginx. This ensures the new port is active without disrupting other services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
