【学习笔记】启动Nginx、查看nginx进程、查看nginx服务主进程的方式、Nginx服务可接受的信号、nginx帮助命令、Nginx平滑重启、Nginx服务器的升级,


1.启动nginx的方式:

cd /usr/local/nginx

ls

./nginx -c nginx.conf

2.查看nginx的进程方式:

[root@localhost nginx]# ps –ef | grep nginx

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

root     21094     1  0 07:52 ?        00:00:00 nginx: master process ./nginx -c nginx.conf

nginx    21095 21094  0 07:52 ?        00:00:00 nginx: worker process

root     21270  3019  1 08:05 pts/1    00:00:00 grep nginx

3.查看Nginx服务主进程的方式:

[root@localhost nginx]# cat nginx.pid

21094

这个进程号和上面的进程号是一样的

4.Nginx服务可接受的信号

信号

作用

TERMINT

快速停止Nginx服务

QUIT

平缓停止Nginx服务

HUP

使用新的配置文件启动进程,之后平缓停止原有进程,也就是所谓的平滑重启

USR1

重新打开日志文件,常用于日志切割,在相关章节中会对此进一步说明

USR2

使用新版本的Nginx文件启动服务,之后平缓停止原有Nginx进程,也就是所谓的平滑升级

WINCH

平缓停止worker process,用于Nginx服务器平滑升级

nginx服务主进程发送信号也有两种方法。一种是使用nginx二进制文件

 

另一种方法是使用kill命令发送信号,其使用方法是:

Kill -9 PID

 

也可以通过发信号的方式:

使用TERM信号

[root@localhost nginx]# kill TERM 21094    (其中21094master进程的pid,其中TERM可以换成INT QUIT)

-bash: kill: TERM: arguments must be process or job IDs

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

root     21417  3019  0 08:16 pts/1    00:00:00 grep nginx

 

重启命令

[root@localhost nginx]# ./nginx -c nginx.conf

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

root     21440     1  0 08:18 ?        00:00:00 nginx: master process ./nginx -c nginx.conf

nginx    21441 21440  0 08:18 ?        00:00:00 nginx: worker process

root     21445  3019  2 08:18 pts/1    00:00:00 grep nginx

 

 

5、使用nginx的帮助的方式

nginx -h

[root@localhost nginx]# ./nginx -h

nginx version: nginx/1.10.1

Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

 

Options:

  -?,-h         : this help

  -v            : show version and exit

  -V            : show version and configure options then exit

  -t            : test configuration and exit

  -T            : test configuration, dump it and exit

  -q            : suppress non-error messages during configuration testing

  -s signal     : send signal to a master process: stop, quit, reopen, reload

  -p prefix     : set prefix path (default: /usr/local/nginx/)

  -c filename   : set configuration file (default: /usr/local/nginx/nginx.conf)

  -g directives : set global directives out of configuration file

 

[root@localhost nginx]#

 

通过-v 用来显示Nginx服务器的版本号;

通过-V显示版本号,同事显示相关的配置信息

通过-t检查Nginx服务器配置文件是否有语法错误,可以与-c联用,使输出内容更详细,这对查找配置文件中的语法错误很有帮助,如果检查通过,将显示类似下面的信息

[root@localhost nginx]# pwd

/usr/local/nginx

[root@localhost nginx]# ls

client_body_temp      fastcgi_params.default  koi-win             nginx               proxy_temp           uwsgi_params

fastcgi.conf          fastcgi_temp            logs                nginx.conf          scgi_params          uwsgi_params.default

fastcgi.conf.default  html                    mime.types          nginx.conf.default  scgi_params.default  uwsgi_temp

fastcgi_params        koi-utf                 mime.types.default  nginx.pid           scgi_temp            win-utf

[root@localhost nginx]# ./nginx -t -c nginx.conf

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/nginx.conf test is successful

[root@localhost nginx]#

 

可以执行

[root@localhost nginx]# ./nginx -V

nginx version: nginx/1.10.1

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)

built with OpenSSL 1.0.1c 10 May 2012

TLS SNI support enabled

configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/home/toto/software/nginxdepents/pcre-8.37 --with-zlib=/home/toto/software/nginxdepents/zlib-1.2.8 --with-openssl=/home/toto/software/nginxdepents/openssl-1.0.1c --with-http_stub_status_module --user=nginx --group=nginx

[root@localhost nginx]#

杀死主进程的方式同样可以是:

./nginx SIGNAL nginx.pid

主要最后面一个参数是nginx.pid的路径

 

6Nginx服务的重启

关于Nginx平滑重启,平滑重启是这样一个过程,Nginx服务进程接收到信号后,首先读取新的Nginx配置文件,如果配置语法正确,则启动新的Nginx服务,然后平缓关闭旧的服务进程;如果新的Nginx配置有问题,将显示错误,仍然使用旧的Nginx提供服务。

 

使用以下命令实现Nginx服务的平滑重启

./nginx –g HUP [-c newConfFile]

HUP信号用于发送平滑重启信号。

newConfFile,可选项,用于指定新配置文件的路径

或者,使用新的配置文件代替了旧的配置文件后,使用:

Kill HUP `nginx.pid`    (注意最后一个参数连边的引号;引号里面是nginx.pid的路径)

也可以实现平滑重启。

 

7Nginx服务器的升级

如果要对当前的Nginx服务器进行版本升级,应用新模块,最简单的办法是停止当前Nginx服务,然后开启新的Nginx服务,但这样就会导致在一段时间内,用户无法访问服务器。为了解决这个问题,Nginx服务器提供平滑升级的功能。

平滑升级的过程是这样的,Nginx服务接收到USR2信号后首先将旧的nginx.pid文件(如果在配置文件中更改过这个文件的名字,也是相同的过程)添加.oldbin,变为nginx.pid.oldbin文件;然后执行新版本Nginx服务器的二进制文件启动服务。如果新的服务启动成功,系统中将由新旧两个Nginx服务公用提供Web服务。如果新的服务启动成功,系统中将有新旧两个Nginx服务共同提供Web服务。之后,需要向旧的Nginx服务进程发送WINCH信号,使旧的Nginx服务平滑停止,并删除nginx.pid.oldbin文件。在发送WINCH信号之前,可以随时停止新的Nginx服务。

注意:

为了实现Nginx服务器的平滑升级,新的服务器安装路径应该和旧的保持一致。因此建议用户在安装新服务器之前先备份旧服务器。如果由于某种原因无法保持新旧服务器安装路径一致,则可以先使用以下命令将旧服务器的安装路径更改为新服务器的安装路径:

./nginx –p newInstallPath

其中,newInstallPath为新服务器的安装路径。之后,备份旧服务器,安装新服务器即可。

做好准备工作以后,使用以下命令实现Nginx服务的平滑升级。

./nginx –g USR2

其中USR2信号用于发送平滑升级信号。或者,使用:

Kill USR2 `nginx.pid`   注意,最后一个参数里面是nginx.pid的路径

通过ps –ef | grep nginx查看新的Nginx服务器启动正常,再使用:

./nginx –g WINCH

其中,WINCH信号用于发送平滑停止旧服务信号。或者,使用:

Kill WIN `nginx.pid`

这样就不停止提供Web服务的前提下完成了Nginx服务器的平滑升级。

 

 

 

 

相关内容