1

nginx启动、关闭和重启

Posted by 撒得一地 on 2015年9月20日 in Nginx

1.nginx的启动
假设你的nginx安装目录是/usr/local/nginx,而且你的可执行文件安装在/usr/local/nginx/sbin/nginx下。下面用<prefix>代替/usr/local/nginx
那么nginx下启动命令:

<prefix>/sbin/nginx -c <prefix>/conf/nginx.conf
或
<prefix>/sbin/nginx 

参数"-c"指定配置文件的路径,参数后面的配置文件要根据你自己系统配置文件路径,如果省略"-c"参数,nginx会默认加载安装目录下conf子目录的nginx.conf文件。

2.nginx的停止
我们可以通过ps命令来查看nginx的主进程号:

[root@rhel5 nginx]# ps -ef | grep nginx

root  12015  1  0  13:52 ?   00:00:00 nginx: master process ./nginx ...

www   12305 12015 0 20:40 ?  00:00:00 nginx: worker process                            

root  12309 12278 0 20:40 pts/1  00:00:00 grep nginx

上面有一个进程的信息时"master process",表示它为主进程,另外一个进程信息为"worker process",为子进程。子进程号id为12305。
如果在nginx.conf配置文件中指定了pid文件存放路径,例如:pid /home/lomde/nginx/logs/nginx.pid ,那么该文件存放路径就是nginx
当前主进程所在路径。如果没有指定pid文件存放路径,nginx.pid文件默认存放在nginx安装目录下的logs目录。
我们可以通过下面的方法关闭nginx,命令如下:

从容停止:
(1)kill -QUIT nginx进程号

快速停止
(2)kill -TERM nginx进程号或kill -INT nginx进程号

强制停止所有进程
(3)pkill -9 nginx
注意是pkill不是kill,kill -9 进程号:只能关闭某个进程,而pkill -9 nginx不仅关闭了主进程也把子经常全部关闭。
例子:
[root@rhel5 nginx]# ps -ef | grep nginx

root   12015  1  0 13:52 ?    00:00:00 nginx: master process ./nginx -c ... 

www    12305 12015 0 20:40 ?   00:00:00 nginx: worker process                            

root   12309 12278 0 20:40 pts/1   00:00:00 grep nginx

[root@rhel5 nginx]# kill -TERM 12015
[root@rhel5 nginx]# ps -ef | grep nginx
root   12376 12278  0 21:14 pts/1   00:00:00 grep nginx
说明nginx被关闭了

3.重启启动nginx

方法一:/usr/local/nginx/sbin/nginx -s reload

方法二:加载配置文件
如果改变了nginx的配置文件,想重启nginx,那么要首先加载新的配置文件,命令如下:
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 
如果配置文件加载成功,则会提示:
[root@rhel5 nginx]# ./nginx -t -c ./nginx.conf

nginx: the configuration file /home/lmode/webserver/nginx/./nginx.conf syntax is ok

nginx: configuration file /home/lmode/webserver/nginx/./nginx.conf test is successful

之后,就可以平滑重启nginx:

kill -HUP nginx进程号

当nginx接收到HUP信号时,它会先解析配置文件(如果指定配置文件,就使用指定的,否则使用默认),如果成功
nginx就运行新的工作进程并关闭旧的工作进程。如果配置文件加载失败,nginx将继续使用旧的配置文件进行工作。

总结nginx的信号控制:

TERM,INT 快速关闭
QUIT 从容关闭
HUP 平滑重启,重新加载配置文件
USR1 重新打开日志文件,在切割日志时用途较大
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

标签:, , ,

上一篇:

下一篇:

相关推荐

1 Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注

1 + 8 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.