1、为什么要对 nginx 平滑升级
随着 nginx 越来越流行,并且 nginx 的优势也越来越明显,nginx 的版本迭代也来时加速模式,1.9.0版本的nginx更新了许多新功能,例如 stream 四层代理功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就是运维的工作了。
nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。 这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。
2、nginx 平滑升级原理
多进程模式下的请求分配方式
nginx 默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。
信号的接收和处理
nginx 主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。
3.平滑升级
查看版本
nginx -V
当前版本为1.12.2
安装编译环境
yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel
安装以下所需的环境
yum -y install GeoIP GeoIP-devel GeoIP-data perl-devel perl-ExtUtils-Embed gd-devel libxml2 libxslt-devel gperftools
下载新版本
wget http://nginx.org/download/nginx-1.16.1.tar.gz
解压
tar -xvf nginx-1.16.1.tar.gz -C /usr/local/
切换到解压包下
cd /usr/local/nginx-1.16.1/
查看原有的模块
nginx -V
注意:复制图中黑体部分即可
执行该语句
./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
编译
make
备份
mv /usr/sbin/nginx /usr/sbin/nginx_$(date +%F) //用于以后,以防万一
切换到objs目录下
cd /usr/local/nginx-1.16.1/objs/
拷贝当前目录下的nginx到/usr/sbin下
cp ./nginx /usr/sbin/
拷贝当前目录下的所有以.so结尾的文件到/usr/sbin下
cp ./*.so /usr/sbin/
切换到modules目录下
cd /usr/lib64/nginx/modules/
创建一个新目录,用于存放/usr/lib64/nginx/modules/下的文件
mkdir /tmp/a //创建一个新的目录,用于存放/usr/lib64/nginx/modules/下的文件
将/usr/lib64/nginx/modules/下的文件全部移动到tmp/a
mv ./* /tmp/a
将/usr/local/nginx-1.16.1/objs/下的所有.so全部拷贝到/usr/lib64/nginx/modules/
cp /usr/local/nginx-1.16.1/objs/*.so /usr/lib64/nginx/modules/
编辑/etc/nginx/nginx.conf文件,并将里面的 这条语句include /usr/share/nginx/modules/*.conf; 暂时 注释
vim /etc/nginx/nginx.conf
给nginx发送平滑迁移信号
kill -USR2 `cat /run/nginx.pid`
查看nginx pid,会出现一个nginx.pid.oldbin
ll /run/nginx.pid*
从容关闭旧的Nginx进程
kill -WINCH `cat /run/nginx.pid.oldbin`
此时不重载配置启动旧的工作进程
kill -HUP `cat /run/nginx.pid.oldbin`
结束工作进程,完成此次升级
kill -QUIT `cat /run/nginx.pid.oldbin`
升级完成!
查看当前版本
nginx -v
若需要重启,需要先停止nginx
可能遇到的问题
解决方法
ss -auntpl |grep 80
kill -9 6069 //6069为pid
systemctl start nginx
nginx -v
可能遇到的问题
./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.
解决方法
yum install gperftools
可能遇到的问题
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
解决方法
yum -y install libxml2
yum -y install libxslt-devel
可能遇到的问题
./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
解决方法
yum -y install gd-devel
可能遇到的问题
./configure: error: perl module ExtUtils::Embed is required
解决方法
yum -y install perl-devel perl-ExtUtils-Embed
可能遇到的问题
./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library.
解决方法
yum -y install GeoIP GeoIP-devel GeoIP-data
可能遇到的问题
nginx 服务启动不启动,升级完成之后不能正常使用
解决方法
将nginx的配置文件 加载模块的参数注释掉