Nginx启动、重启、重新加载配置文件、平滑升降版本
-
Nginx有一个主进程和几个工作进程,主进程的主要作用就是
读取、评估配置文件和管理工作进程
,工作进程对请求做实际处理
; -
工作进程的数量是在配置文件中配置的,一般设置为
cpu的核心数*线程数
; -
nginx可以通过信号控制,默认情况下,主进程的进程ID记录在/usr/local/nginx/logs/nginx.pid 文件中;
-
信号可以通过nginx可执行文件发送给nginx (nginx -s 信号),也可以通过unix的kill命令发送给nginx,这种情况下是将信号直接发送给指定进程ID的进程,如:kill QUIT 1628;
一、Nginx服务常用管理命令
1、启动nginx,直接运行nginx的可执行文件:
[root@localhost ~]# /usr/local/nginx/sbin/nginx
2、停止nginx
- 快速关闭:
nginx -s stop
或者kill -TERM 主进程号
或者kill -INT 主进程号
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
或者
[root@localhost ~]# ps -ef | grep nginx
root 9970 1 0 10:12 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 9971 9970 0 10:12 ? 00:00:00 nginx: worker process
root 10195 9921 0 10:24 pts/2 00:00:00 grep --color=auto nginx
[root@localhost ~]# kill -TERM 9879
- 优雅关闭(不接受新的连接请求,等待旧的连接请求处理完毕再关闭):
nginx -s quit
或者kill -QUIT 主进程号
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s quit
或者
注意:执行该命令的用户应该是启动nginx的用户
[root@localhost sbin]# ps -ef |grep nginx
root 9889 1 0 13:17 ? 00:00:00 nginx: master process ./nginx
nobody 9890 9889 0 13:17 ? 00:00:00 nginx: worker process
root 9893 2008 0 13:18 pts/1 00:00:00 grep nginx
[root@localhost sbin]# kill -QUIT 9889
3、重新加载配置文件
nginx -s reload
或者 kill -HUP 主进程号
nginx配置平滑更新
为了让主进程重新读取配置文件,应该向主进程发送一个HUP信号,主进程一旦接收到重新加载配置的的信号,它就检查配置文件语法的有效性,然后试图应用新的配置,即打开新的日志文件和新的socket 监听,如果失败,它将回滚配置更改并继续使用旧的配置,如果成功了,它开启新的工作进程,并给旧的工作进程发消息让它们优雅的关闭,旧的工作进程接收到关闭信号后,不再接收新的请求,如果已有请求正在处理,等当前请求处理完毕后关闭,如果没有请求正在处理,则直接关闭。
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
或者
[root@localhost sbin]# ps -ef|grep nginx
root 9944 1 0 13:22 ? 00:00:00 nginx: master process ./nginx
nobody 9949 9944 0 13:23 ? 00:00:00 nginx: worker process
root 9960 9917 0 13:28 pts/1 00:00:00 grep nginx
[root@songguoliang sbin]# kill -HUP 9944
4、测试配置文件,检查配置文件语法是否正确
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5、查看nginx版本信息
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.8.0
6、查看nginx版本信息,编译版本,和配置参数
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.8.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi
7、重启日志文件,备份日志文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reopen
二、Nginx平滑升降级版本
Nginx Web 软件定期更新,以下为将低版本升级或者将高版本降级的方法,一般分为四个步骤:软件下载、预编译、编译、配置
,具体方法及代码如下:
1、Nginx软件包下载
[root@localhost ~]# wget http://www.nginx.org/download/nginx-1.4.2.tar.gz
2、获取旧版本configure信息
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.8.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi
3、编译新版本Nginx
[root@localhost ~]# tar - xvf nginx-1.4.2.tar.gz
[root@localhost ~]# cd nginx-1.4.2
编译内容跟老版本一致
[root@localhost nginx-1.4.2]# ./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi
[root@localhost nginx-1.4.2]# make
不要执行 make install
命令!!!
4、备份旧版本的 Nginx 可执行文件,复制新版本的 Nginx 可执行文件
[root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@localhost ~]# cp objs/nginx /usr/local/nginx/sbin/
5、测试新版本 Nginx 是否可以正常使用
[root@localhost ~]# /usr/local/nginx/sbin/nginx - t
6、平滑重启升级 Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
7、验证 Nginx 是否升级成功
[root@localhost ~]# /usr/local/nginx/sbin/nginx - v
///最新版本号
[root@localhost ~]# /usr/local/nginx/sbin/nginx - V
///最新编译内容