侧边栏壁纸
  • 累计撰写 416 篇文章
  • 累计创建 65 个标签
  • 累计收到 154 条评论

目 录CONTENT

文章目录

Nginx 在线版本升级,升级到最新v1.22.0版本

Z同学
2022-09-25 / 0 评论 / 6 点赞 / 4,621 阅读 / 1,523 字
温馨提示:
本文最后更新于 2022-09-27,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1. 介绍

前面介绍过Nginx 隐藏版本(https://zinyan.com/?p=339)提高安全。

但是不管怎么配置,都没有直接进行版本升级来的安全性高。下面就来介绍我是如何将服务器的nginx版本升级到最新的v1.22.0

由于我的服务器是CentOS 下面主要围绕CentOS下的Nginx 版本升级进行的介绍。

2. yum 在线升级

2.1 确认Nginx版本

通过 nginx -v 命令查询本机安装的nginx 版本,实例:

[root@xxxxxuf63 /]# nginx -v
nginx version: nginx/1.14.1

可以通过http://nginx.org/en/download.html 官网,知道nginx 当前的最新版本。

或者通过 yum list |grep nginx 可以查询到最新的版本。

[root@xxxxxuf63 /]# yum list |grep nginx

知道版本后,就可以判断是否进行更新操作了。

2.2 配置在线升级

根据官网的介绍,我们可以通过 vim /etc/yum.repos.d/nginx.repo 文件,配置以下内容:

[nginx-stable]   
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

有一些文章,会让我们将$releasever 改为系统版本等等。其实这一步大可不必。而且修改后update更新功能将无法使用。

2.3 更新nginx

配置完毕后,我们可以通过yum update nginx -y 实现在线升级了。

为了避免升级可能造成的配置文件丢失问题。我们可以针对当前的nginx的配置进行备份。

通过 nginx -V 可以查询到本机安装的nginx的各种配置信息,实例如下:

[root@xxxxxuf63 /]# nginx -V
nginx version: nginx/1.14.1
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 
built with OpenSSL 1.1.1 FIPS  11 Sep 2018 (running with OpenSSL 1.1.1c FIPS  28 May 2019)
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_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_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-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
  • prefix :前置页面的存储地址。例如 异常页面40x.html, 50x.html 等。
  • sbin-path:nginx执行文件的存储地址。(我们更新也会更新里面的内容)
  • conf-path: 配置项存储地址,也就是nginx.conf 的文件地址。
  • error-log-path: 错误日志的存地址等等。

而我们通常修改项都在conf-path路径下。

2.3.1 备份 conf-path

如果担心可以通过:cp -r 进行备份操作。后面跟着两个文件名,第一个名是要备份的文件,第二个是复制后新文件名。

这就是一个复制操作。(PS:我的nginx 就在etc目录下,就直接写nginx了。如果你并不是在etc目录下,需要写路径否则会找不到文件对象)。

[root@xxxxxuf63 etc]# cp -r nginx nginx-1.14.1

例如上面,我就将nginx 复制了一个nginx-1.14.1的文件夹。

之后通过:yum update nginx -y 进行升级即可。 升级完毕后可以通过nginx -v 看到新版本是否正确了。

实例:

[root@xxxxxuf63 etc]# sudo yum update nginx -y
nginx stable repo                                                                                                                             48 kB/s |  43 kB     00:00    
Dependencies resolved.
=============================================================================================================================================================================
 Package                             Architecture                         Version                                           Repository                                  Size
=============================================================================================================================================================================
Upgrading:
 nginx                               x86_64                               1:1.22.0-1.el8.ngx                                nginx-stable                               827 k

Transaction Summary
=============================================================================================================================================================================
Upgrade  1 Package
......                                                                                                                            
Complete!

直到出现 complete! 就代表更新完毕了。

2.4 检查并重启

安装完毕后,可以通过nginx -v 检查版本是否为最新版本了,实例:

[root@xxxxxuf63 etc]# nginx -v
nginx version: nginx/1.22.0

然后我们可以通过nginx -t 测试一下conf配置文件是否通过。实例:

[root@xxxxxuf63 etc]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

出现successful 就代表通过检测。我们的nginx可以启动了。

一切完毕后,通过nginx-s reload 重启即可完成整体的升级操作了。实例:

[root@xxxxxuf63 nginx]# nginx -s reload

到这里安装就结束了。

2.4 卸载重装

我们如果更新或者安装出现了无法解决的问题,可以尝试卸载后重装的模式解决。

1、卸载: yum remove nginx

2、安装: yum install nginx -y

3、启动: service nginx restart

建议大家安装时采用yum进行安装。这样更新和后续的卸载等也会很方便。

3. listen … ssl 错误问题

在升级到1.22.0版本后,检测conf文件时出现了ssl 错误。(主要是因为开启了https)

nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/nginx.conf:29

那是因为v1.22.0版本ssl的写法改变了。

#老版本
listen 443;
ssl on;

#新版本
listen 443 ssl;

新版本将ssl 直接写在 端口后面了。同时可以去掉ssl on配置项。

参考链接:http://nginx.org/en/linux_packages.html

6

评论区