F5社区-F5技术交流中心

博文精选 | nginx 平滑升级、以及导入第三方模块

2022-04-27 10:43:16

F5小安

PHPWord

文章速览:

 

行业:互联网

 

关键字:nginx、平滑升级、第三方模块、配置

 

摘要:nginx 平滑升级、以及导入第三方模块

 

阅读时长:5分钟

 

 

以下文章来源于InfoQ!作者:sinsy

 

 

nginx 平滑升级、以及导入第三方模块

如何在生产环境中不停机升级 nginx、或者不停机引入 nginx 第三方模块


前言

在引入第三方模块 pagespeed 的时候,查阅了官网发现,引入第三方需要重新安装升级,还有就是通过 yum install 加载进去。

不过试了一下,发现不能,最后才定位到。首先你得配置了 configure arguments。里面有你配置的模块才行,不然还是得重新安装一个。

[root@localhost ~]# nginx -V
复制代码

-V 打印 nginx 版本,编译器版本和配置参数。输出如下

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_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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-http_auth_request_module --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'
复制代码

正文

先看看当前的版本



当前的版本为 nginx version: nginx/1.16.1

下载一个最新版本

[root@localhost ~]# wget http://nginx.org/download/nginx-1.19.4.tar.gz
复制代码

解压同时进去

[root@localhost ~]# tar -vxf nginx-1.19.4.tar.gz[root@localhost ~]# cd nginx-1.19.4
复制代码

里面有个 configure 可执行文件。

要保证之前的版本和现在下载的配置一致。所以将 nginx -V 输出的东西复制一下,除了 configure arguments: 不用复制。

在这一步就可以导入第三方的模块,比如现在导入第三方模块叫 pagespeed,最后面加个--add-module=/usr/local/pagespeed-ngx

[root@localhost nginx-1.19.4]# ./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_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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-http_auth_request_module --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' --add-module=/usr/local/pagespeed-ngx
复制代码

你之前的 gcc 编译器可能版本比较低,可能出现 ./configure: error: C compiler cc is not found 的错误。执行。

[root@localhost nginx-1.19.4]# yum -y install gcc-c++
复制代码

之后如果还有 error 的错误,大概是模块不存在,你可以通过 yum install 下载你需要的依赖。

出现

creating objs/MakefileConfiguration summary  + using system PCRE library  + using system OpenSSL library  + using system zlib library  nginx path prefix: "/usr/share/nginx"  nginx binary file: "/usr/sbin/nginx"  nginx modules path: "/usr/lib64/nginx/modules"  nginx configuration prefix: "/etc/nginx"  nginx configuration file: "/etc/nginx/nginx.conf"  nginx pid file: "/run/nginx.pid"  nginx error log file: "/var/log/nginx/error.log"  nginx http access log file: "/var/log/nginx/access.log"  nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"  nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"  nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"  nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"  nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"
复制代码

这表示你安装成功。

然后在同个目录底下执行 make,不要执行 make install 。如果执行了会覆盖之前的文件。

[root@localhost nginx-1.19.4]# make[root@localhost nginx-1.19.4]# cd objs/
复制代码

输入 ./nginx -V 查看版本是否是你安装的版本、以及第三方模块是否导入成功

[root@localhost objs]# ./nginx -V
复制代码

结果输入如下:


由于是在生产环境升级部署。所以备份之前的版本很重要,如果是个人测试可以跳过这一步。

先找到你之前版本的二进制文件。

你可以 whereis nginx,也可以看 nginx -V 里面的 --sbin-path 路径。

# 二进制文件备份[root@localhost objs]# cd /usr/sbin[root@localhost sbin]# cp nginx nginx.20201117.old# 配置文件备份[root@localhost sbin]# cd /etc[root@localhost nginx]# cp -r nginx nginx.20201117.old
复制代码

现在可以替换了。

[root@localhost etc]# cd /root/nginx-1.19.4/objs/[root@localhost objs]# rm -frv /usr/sbin/nginx[root@localhost objs]# mv nginx /usr/sbin/[root@localhost objs]# nginx -V[root@localhost objs]# nginx -s reload[root@localhost objs]# nginx -t
复制代码

输出的版本对上,那么应该就没事问题了,然后重新加载 nginx,以及测试语法有没有错误。

nginx: the configuration file /tmp/to_remove/conf/nginx.conf syntax is oknginx: configuration file /tmp/to_remove/conf/nginx.conf test is successful
复制代码

输出上图则表示升级成功。

不过这里有一个坑,那就是版本差距太大,会出现依赖版本冲突。我这边的做法是,遇到了这个,我则会执行 make install 覆盖之前的版本。然后在将备份的 nginx nginx.20201117.old 底下的 nginx.conf 文件,重新写入新的 nginx.conf 文件中。

做法如下:

[root@localhost ~]# cd /root/nginx-1.19.4[root@localhost nginx-1.19.4]# make install
复制代码

自此完成了 nginx 升级以及 nginx 第三方模块的引入。

可以继续使用

如果后续还想添加第三方依赖,或者修改 configure arguments:

[root@localhost ~]# cd /root/nginx-1.19.4[root@localhost nginx-1.19.4]# make clean
复制代码

然后,在重复之前的操作 ./configure、make 等命令,替换二进制的 nginx 文件。

声明

作者: Sinsy

本文链接:https://blog.sincehub.cn/2020/11/17/upgrade-nginx/

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文声明。

 

以上是针对nginx 升级及导入第三方模块的分享,希望对大家有帮助!

 

 

 

阅读原文

 

声明:本文章版权归原作者及原出处所有 。凡本社区注明“来源:XXX或转自:XXX”的作品均转载自其它媒体,转载目的在于传递分享更多知识,内容为作者个人观点,仅供参考,并不代表本社区赞同其观点和对其真实性负责。本社区转载的文章,我们已经尽可能的对作者和来源进行了注明,若因故疏忽,造成漏注,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本社区拥有对此声明的最终解释权。

发布评论 加入社群

发布评论

相关文章

博文精选 | nginx 性能优化 -- 配置解析

F5小安

2022-07-21 10:35:06 512

博文精选 | 漫画 Nginx 的 subfilter

F5小安

2022-05-27 13:57:18 409

博文精选 | 一篇文章搞定 Nginx 反向代理与负载均衡

F5小安

2022-05-26 10:31:14 917

Login

手机号
验证码
© 2019 F5 Networks, Inc. 版权所有。京ICP备16013763号-1