千家信息网

nginx的平滑升级

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,$$$$$$$$$$$$$$$$$$$$$$$$$$$$理论大概$$$$$$$$$$$$$$$$$$$$$$$一、为什么要对 nginx 平滑升级随着 nginx 越来越流行,并且 nginx 的优势
千家信息网最后更新 2026年11月03日nginx的平滑升级

$$$$$$$$$$$$$$$$$$$$$$$$$$$$理论大概$$$$$$$$$$$$$$$$$$$$$$$

一、为什么要对 nginx 平滑升级
随着 nginx 越来越流行,并且 nginx 的优势也越来越明显,nginx 的版本迭代也来时加速 模式,1.9.0版本的nginx更新了许多新功能,例如 stream 四层代理功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就是运维的工作了。

二、nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。 这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。

三、nginx 平滑升级原理
多进程模式下的请求分配方式
nginx 默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。
信号的接收和处理
nginx 主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。

四、Nginx信号简介:
主进程支持的信号
• TERM, INT: 立刻退出
• QUIT: 等待工作进程结束后再退出
• KILL: 强制终止进程
• HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
• USR1: 重新打开日志文件
• USR2: 启动新的主进程,实现热升级
• WINCH: 逐步关闭工作进程

工作进程支持的信号
• TERM, INT: 立刻退出
• QUIT: 等待请求处理结束后再退出
• USR1: 重新打开日志文件

%%%%%%%%%%%%%%%%%%%nginx 平滑升级的过程%%%%%%%%%%%%%%%%%%

1、yum安装的nginx升级,以当前nginx为nginx-1.12.2版本升级到nginx-1.16.1为例

 yum -y install nginx          (为什么可以直接yum,因为epel扩展源里存在nginx-1.12.2)systemctl start nginxsystemctl status nginx      (确定已经开启nginx)     nginx -V                        检测版本与配置参数

nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --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_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

2,开始升级版本由nginx-1.12.2升级到nginx-1.16.1版本。按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖

wget http://nginx.org/download/nginx-1.16.1.tar.gz     

tar xzf nginx-1.16.1.tar.gz -C /usr/local 解压压缩包到指定位置

yum -y install GeoIP GeoIP-devel GeoIP-data perl-devel perl-ExtUtils-Embed gd-devel libxml2 libxslt-devel gperftools

yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel 添加环境

cd /usr/local ( cd到解压后的目录 )
ls
cd nginx-1.16.1/

[root@localhost nginx-1.16.1]#nginx -V (配置参数前先查看原来版本的参数)
[root@localhost nginx-1.16.1]#./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --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_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug
(配置升级版本的参数要与原版本相同的参数,复制到--with-debug 即可,其后的--with cc的都不需要)

make

3,备份原 nginx 二进制文件 (yum安装的nginx,其二进制文件和配置文件默认存放于/usr/sbin/)备份二进制文件和 nginx 的配置文件(期间nginx不会停止服务)
mv /usr/sbin/nginx /usr/sbin/nginx_$(date +%F)

4,复制新版本的nginx二进制文件,进入新的nginx源码包
cp /usr/local/nginx-1.16.1/objs/nginx /usr/sbin/

5,测试新版本的nginx是否正常
/usr/sbin/nginx -t
若出现如下报错:
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-geoip.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
解决方法:
#vim /etc/nginx/nginx.conf (编辑nginx的主配置文件)
找到#include /usr/share/nginx/modules/*.conf;这一句并将其注释即可

/usr/sbin/nginx -t (再次测试便不会在报错)

6、给nginx发送平滑迁移信号(若不清楚pid路径,请查看/etc/nginx/nginx.conf配置文件)
kill -USR2 cat /var/run/nginx.pid

(查看pid也可find / -name "nginx.pid")

7.查看nginx pid,会出现一个nginx.pid.oldbin
[root@localhost nginx-1.16.1]# ll /var/run/nginx.pid*
-rw-r--r--. 1 root root 5 8月 24 01:30 /var/run/nginx.pid
-rw-r--r--. 1 root root 5 8月 24 01:30 /var/run/nginx.pid.oldbin

8、从容关闭旧的Nginx进程
#kill -WINCH cat /var/run/nginx.pid.oldbin

9、此时不重载配置启动旧的工作进程
#kill -HUP cat /var/run/nginx.pid.oldbin

10、结束工作进程,完成此次升级
#kill -QUIT cat /var/run/nginx.pid.oldbin

11、验证Nginx是否升级成功
nginx -V
nginx version: nginx/1.16.1 版本升级成功为nginx-1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled

进程 升级 文件 版本 配置 工作 平滑 参数 处理 信号 二进制 模式 越来越 越来 成功 原理 备份 就是 情况 方法 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 手冲咖啡和咖啡机区别 南京欣网互联网络科技 互联网医疗科技 杭州互联网科技公司 智慧科技互联网 米客互联网科技 万达互联网科技 互联网科技英文翻译 左右互联网络科技 互联网 科技服务平台 天速互联网络科技有限公司 赶超互联网科技 微信多开 深圳市罗太互联网科技有限公司 从那以后互联网科技有限公司 扫呗互联网科技有限公司 九思连强互联网电子科技有限公司 广东鹅鹅鹅互联网科技有限公司 中国新注册互联网科技 科技互联网专业工作会议发言稿 互联网领先科技成果宣传视频 南京欣网互联网络科技 盈实互联网科技 互联网科技有限公司 翻译 居家互联网科技有限公司 青岛科技大学互联网大赛研究生 链环科技传统企业怎么转型互联网 天津腾达互联网科技有限公司 21世纪互联网科技成就 科技互联网经济论坛 玉溪新华互联网科技怎么选
0