nginx 平滑升级

简介: 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

目录
相关文章
|
22天前
|
应用服务中间件 网络安全 nginx
nginx 常用命令 |升级到1.20.1版本 | 如何更换 Nginx SSL 证书
nginx 常用命令 |升级到1.20.1版本 | 如何更换 Nginx SSL 证书
198 0
|
22天前
|
缓存 应用服务中间件 网络安全
nginx服务升级配置
nginx服务升级配置
|
8月前
|
应用服务中间件 nginx
Nginx的安装与平滑升级(详)
Nginx的安装与平滑升级(详)
117 0
|
9月前
|
域名解析 负载均衡 JavaScript
升级 HTTP 至 HTTPS:使用 Nginx 反向代理
升级 HTTP 至 HTTPS:使用 Nginx 反向代理
218 0
|
11月前
|
Kubernetes 应用服务中间件 nginx
基于 Kubernetes 进行 Nginx 的升级与回滚
Hello folks,今天我们介绍一下如何在 Kubernetes 集群环境中进行服务组件的升级与回滚,此处,我们以 Nginx 组件为例,基于 K3d 所搭建的环境进行。
129 0
|
11月前
|
应用服务中间件 nginx
Nginx专题:Nginx软件升级
Nginx专题:Nginx软件升级
105 0
|
Web App开发 应用服务中间件 Linux
Nginx的深思:如何优雅告知用户,网站正在升级维护?
对的,我升级Lv3了,这是对自己坚持写作1年多的认可与鼓励,难掩心中的开心,我就去发了个掘金沸点,纪念下这个时刻,然后就继续工作了。 中午12点出去吃完饭回到座位后,再次打开掘金首页,哎呀,访问不了了,提示如下:
|
Kubernetes 负载均衡 Java
Kubeadm 升级 k8s 至 v1.17.4及运行 nginx+tomcat 并实现动静分离 | 学习笔记
快速学习 Kubeadm 升级 k8s 至 v1.17.4及运行 nginx+tomcat 并实现动静分离
283 0
|
安全 应用服务中间件 网络安全
Nginx HTTPS 实现、自定义 Server 名称及升级 OpenSSL(二)|学习笔记
快速学习 Nginx HTTPS 实现、自定义 Server 名称及升级 OpenSSL
157 0
|
应用服务中间件 nginx
使用 Nginx 实现平滑升级
使用 Nginx 实现平滑升级
223 0
使用 Nginx 实现平滑升级