1、nginx平滑升级
1.1 平滑升级的应用场景和意义
Nginx平滑升级具有重要意义与广泛的应用场景。在意义层面,它可在不中断服务的前提下完成版本更新,避免因服务暂停导致的用户访问中断、业务损失及影响用户体验,同时能让新版本的功能、性能优化和安全补丁及时生效,增强服务器稳定性与安全性,还可减少运维操作复杂度和对业务的干扰,提升系统可用性。在应用场景方面,当Nginx新版本引入关键功能(如更高效的负载均衡算法、新协议支持)时,平滑升级能让业务快速受益;面对已知安全漏洞,可通过平滑升级及时修复,降低被攻击风险;当业务流量增长,需利用新版本性能优化(如连接处理优化、内存管理改进)提升服务器处理能力时,平滑升级可在不中断服务的情况下实现;此外,在需要对Nginx配置进行重大调整或模块升级时,平滑升级能确保调整过程中服务持续可用,避免因配置变更导致的服务中断问题。
2、平滑升级的操作
2.1 安装 nginx1.20.2
#拖入相关的安装包
24 yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
#安装依赖环境
25 useradd -M -s /sbin/nologin nginx
#创建nginx用户便于管理
26 ls
27 tar -xf nginx-1.20.2.tar.gz
28 ls
29 cd nginx-1.20.2/
30 l
31 ls
32 ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#指定编译路径,以及可能用得到的模块
33 vim /etc/profile
34 source /etc/profile
#声明nginx的环境变量
35 nginx -v
36 vim /etc/profile
37 ls
38 make -j2 && make install
#编译安装
39 nginx -v
[root@localhost nginx-1.20.2]#
查看安装的版本为1.20.2
[root@localhost nginx-1.20.2]# nginx -v
nginx version: nginx/1.20.2
[root@localhost nginx-1.20.2]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.20.2]#
2.2 平滑升级操作
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
nginx-1.28.0.tar.gz rh
#拖入安装包
[root@localhost opt]# tar -xf nginx-1.28.0.tar.gz
[root@localhost opt]# ls
nginx-1.28.0 nginx-1.28.0.tar.gz rh
[root@localhost opt]# cd nginx-1.28.0/
[root@localhost nginx-1.28.0]# ls
auto CODE_OF_CONDUCT.md contrib LICENSE SECURITY.md
CHANGES conf CONTRIBUTING.md man src
CHANGES.ru configure html README.md
[root@localhost nginx-1.28.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#需要和上面低版本的模块一样
编译(注意只能编译)make -j2
[root@localhost nginx-1.28.0]# ls
auto CODE_OF_CONDUCT.md contrib LICENSE objs src
CHANGES conf CONTRIBUTING.md Makefile README.md
CHANGES.ru configure html man SECURITY.md
[root@localhost nginx-1.28.0]# make -j2
复制nginx的执行文件到相应的位置
[root@localhost nginx-1.28.0]# ls
auto CODE_OF_CONDUCT.md contrib LICENSE objs src
CHANGES conf CONTRIBUTING.md Makefile README.md
CHANGES.ru configure html man SECURITY.md
[root@localhost nginx-1.28.0]# cd objs/
#进入obj目录
[root@localhost objs]# ls
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
#找到nginx的执行文件
[root@localhost objs]# cd /apps/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# mv nginx nginx.old
#备份原来的执行文件为nginx.old
[root@localhost sbin]# cd -
/opt/nginx-1.28.0/objs
[root@localhost objs]# ls
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
[root@localhost objs]# mv nginx /apps/nginx/sbin/
#把高版本的nginx可执行文件移动到低版本的工作目录
[root@localhost objs]# ls /apps/nginx/sbin/
nginx nginx.old
[root@localhost objs]#
make upgrade操作
[root@localhost objs]# cd /opt/nginx-1.28.0/
[root@localhost nginx-1.28.0]# ls
auto CODE_OF_CONDUCT.md contrib LICENSE objs src
CHANGES conf CONTRIBUTING.md Makefile README.md
CHANGES.ru configure html man SECURITY.md
[root@localhost nginx-1.28.0]#
#需要切换到nginx的文件夹目录
[root@localhost nginx-1.28.0]# make upgrade
/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /apps/nginx/logs/nginx.pid`
sleep 1
test -f /apps/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
[root@localhost nginx-1.28.0]# nginx -v
nginx version: nginx/1.28.0
[root@localhost nginx-1.28.0]#
#此时我们就完成了升级
2.3 平滑回滚
在上面的基础上,回滚操作就显得很容易了
[root@localhost nginx-1.28.0]# cd /apps/nginx/sbin/
[root@localhost sbin]# ls
nginx nginx.old
#进入目录
[root@localhost sbin]# mv nginx nginx.1.28
[root@localhost sbin]# ls
nginx.1.28 nginx.old
[root@localhost sbin]# mv nginx.old nginx.
[root@localhost sbin]# mv nginx. nginx
[root@localhost sbin]# ls
nginx nginx.1.28
#更改nginx可执行文件的名称
[root@localhost sbin]# ls
nginx nginx.1.28
[root@localhost sbin]# cd /opt/nginx-1.28.0/
#进入nginx文件目录
[root@localhost nginx-1.28.0]# make upgrade
/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /apps/nginx/logs/nginx.pid`
sleep 1
test -f /apps/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
#make upgrade成功
[root@localhost nginx-1.28.0]# nginx -v
nginx version: nginx/1.20.2
#查看版本是否回滚成功
[root@localhost nginx-1.28.0]#
3、总结
通过nginx的执行文件可以决定nginx运行的版本,更换nginx的执行文件可以实现nginx版本的升级和回滚。