linux重新加载nginx配置的三种办法

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         # 暴力干掉进程
posted @ 2024-08-09 22:09  先ping一下网关  阅读(769)  评论(0编辑  收藏  举报