linux重新載入nginx配置的三種辦法

一个不愿抽烟的白嫖客發表於2024-08-09

1.確保當前nginx程序執行中

[root@master10 ~]# 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)
  Process: 1434 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1432 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (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 process

Aug 09 17:26:42 master10 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Aug 09 17:26:42 master10 nginx[1434]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Aug 09 17:26:42 master10 nginx[1434]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Aug 09 17:26:42 master10 systemd[1]: Started The nginx HTTP and reverse proxy server.

2.檢視nginx當前監聽埠

[root@master10 ~]# 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: master 

3.修改nginx監聽埠為12111

[root@master10 ~]# vim /etc/nginx/nginx.conf

第39行改為12111;
儲存後還未生效,需重啟服務生效;

[root@master10 ~]# 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: master 

4.第一種方法,利用systemctl命令重啟服務載入

[root@master10 ~]# systemctl restart nginx
[root@master10 ~]# 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: master 

重啟後檢視nginx當前監聽埠已變為12111

5.再次修改nginx監聽埠為12112
重複第3步 將埠改為12112

6.第二種方法、利用nginx命令過載

[root@master10 ~]# nginx -s reload
[root@master10 ~]# 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: master

透過 nginx -s reload重啟,然後檢視nginx當前監聽埠已變為12112
-s 引數解釋如下,意思為向主程序傳送一個訊號,引數訊號可以是:停止、退出、重新開啟、重新載入

-s signal      Send a signal to the master process.  The argument signal can be one of: stop, quit,
                    reopen, reload.  The following table shows the corresponding system signals:

                    stop    SIGTERM
                    quit    SIGQUIT
                    reopen  SIGUSR1
                    reload  SIGHUP

7.再再次修改nginx監聽埠為12113
再再次重複第3步,將埠改為12113

8.第三種方法、利用kill命令過載

[root@master10 ~]# vim /etc/nginx/nginx.conf
[root@master10 ~]# ps -ef | grep nginx
root       2994      1  0 21:48 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      3032   2994  0 21:53 ?        00:00:00 nginx: worker process
nginx      3033   2994  0 21:53 ?        00:00:00 nginx: worker process
root       3114   1384  0 22:01 pts/0    00:00:00 grep --color=auto nginx
[root@master10 ~]# kill -1 2994
[root@master10 ~]# 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: master  
[root@master10 ~]# 

利用kill命令過載需要提前用ps -ef | grep nginx 獲取nginx主程序的程序號,然後透過kill 訊號引數 pid對程序進行控制,透過 netstat -tunlp | grep nginx可見監聽埠已變為12113.
常見的kill訊號引數可以透過kill -l檢視

[root@master10 ~]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

常用的有

kill -1 pid         # 程序重讀配置檔案
kill -15 pid         # 程序優雅退出
kill -9 pid         # 暴力幹掉程序

相關文章