nginx平滑升级

 

 

nginx版本:

旧的:nginx/1.16.1

新的:nginx/1.18.0

下载新版本nginx

nginx下载页面

http://nginx.org/en/download.html

Linux:~ # cd /usr/local/src/
Linux:/usr/local/src # wget http://nginx.org/download/nginx-1.18.0.tar.gz
Linux:/usr/local/src # tar xf nginx-1.18.0.tar.gz

编译新版本nginx

新版本的nginx编译,切勿直接复制我的文档,要根据自身环境所需功能进行编译

使用 nginx -V 可以获取旧版本nginx在编译时所安装的模块(注意:V大写的)

Linux:/usr/local/src # cd nginx-1.18.0/
Linux:/usr/local/src/nginx-1.18.0 # ./configure --prefix=$(pwd)/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module && \
make && make install

升级新版本nginx

查看当前nginx进程pid号

Linux:~ # ps -ef | grep nginx
root      40552      1  0 15:21 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     40553  40552  0 15:21 ?        00:00:00 nginx: worker process
root      40578   1550  0 15:24 pts/0    00:00:00 grep --color=auto nginx
  • 这里可以看到,我的nginx所在路径是 /usr/local/nginx

备份旧版本nginx二进制文件

  • 如果是生产环境,nginx集群前面有反向代理服务器,需要先stop掉nginx服务
    • 因为反向代理对整个服务器集群是有健康检查的
      • 如果健康检查发现了某台服务器已经DOWN机,就会将机器从反向代理中摘除,这样就不会有生产环境的流量引入了
      • 当健康检查检测到服务器UP后,会再将这台服务器添加到反向代理上,这之后生产流量才会引入
Linux:~ # mv /usr/local/nginx/sbin/nginx{,-1.16.1}

替换新版本nginx二进制文件

Linux:~ # cp /usr/local/src/nginx-1.18.0/nginx/sbin/nginx /usr/local/nginx/sbin/
Linux:~ # /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0

注意自己的nginx二进制文件所在的路径,不要做复制黏贴工程师

这里可以看到,nginx的版本为1.18.0

检查nginx配置文件兼容性

Linux:~ # /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/src/nginx-1.18.0/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/src/nginx-1.18.0/nginx/conf/nginx.conf test is successful

平滑停止原有的nginx进程

Linux:~ # kill -USR2 40552
Linux:~ # ps -ef | grep nginx
root      40552      1  0 15:21 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     40553  40552  0 15:21 ?        00:00:00 nginx: worker process
root      40698  40552  0 15:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     40699  40698  0 15:54 ?        00:00:00 nginx: worker process
root      40733   1550  0 15:58 pts/0    00:00:00 grep --color=auto nginx

如果需要回滚的话:
1、替换nginx二进制文件后
2、使用kill -USR1 40552

平缓停止旧服务的worker process

Linux:~ # kill -WINCH 40552
Linux:~ # ps -ef | grep nginx
root      40552      1  0 15:21 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
root      40698  40552  0 15:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     40699  40698  0 15:54 ?        00:00:00 nginx: worker process
root      40780   1550  0 16:02 pts/0    00:00:00 grep --color=auto nginx
  • 可以看到nginx: worker process的pid号已经变了

验证升级是否成功

Linux:~ # curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 31 Mar 2021 08:05:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 31 Mar 2021 07:14:00 GMT
Connection: keep-alive
ETag: "60642138-264"
Accept-Ranges: bytes
  • 可以看到server字段的nginx版本为1.18.0

关闭老的master进程

  • 如果在版本升级完成后,没有任何问题,需要关闭老的master进程的话,可以使用下面的命令
Linux:~ # kill -QUIT 40552(旧版本master的pid)

关于文档里面出现的kill信号USR和QUIT:

kill -l可以列出所有的kill信号

Linux:~ # 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

信号相关的资料,有兴趣的,可以查阅github:SIGNALS-IN-LINUX

posted @ 2021-03-31 16:44  月巴左耳东  阅读(72)  评论(0)    收藏  举报