将 Nginx 1.12.2 升级到 1.14.1 (修复 Dos 攻击漏洞 )

nginx 官方提供了 rpm 包,无论是 CentOS6 还是 CentOS7,我们都可以直接使用 yum 来安装或升级,免去了新版本需要编译安装的麻烦。

将 Nginx 1.12.2 升级到 1.14.1 (修复 Dos 攻击漏洞 )

升级过程

CentOS7.x 自带 nginx 1.12.2,已经默认开启了 mail 和 stream(tcp代理)模块支持。

由于这个 HTTP/2 漏洞会影响 1.9.5 和 1.15.5 之间的所有 nginx 版本,我们需要升级 nginx 到 Stable version 1.14.1 或者 Mainline version 1.15.6 。

漏洞详情:https://www.oschina.net/news/101630/nginx-security-issues

nginx 官方提供了 rpm 包,我们可以直接使用 yum upgrade 来升级。

rpm 地址: https://nginx.org/packages/centos/7/x86_64/RPMS

nginx -v
# nginx version: nginx/1.12.2

# 升级 nginx
yum -y upgrade https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.1-1.el7_4.ngx.x86_64.rpm


# 升级相应模块
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-geoip-1.14.1-1.el7_4.ngx.x86_64.rpm
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-image-filter-1.14.1-1.el7_4.ngx.x86_64.rpm
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-perl-1.14.1-1.el7_4.ngx.x86_64.rpm
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-xslt-1.14.1-1.el7_4.ngx.x86_64.rpm

# 检查升级后版本
nginx -v
# nginx version: nginx/1.14.1

# 查看所支持的模块
nginx -V
nginx version: nginx/1.14.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: \
--prefix=/etc/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 \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong \
--param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

问题处理

一开始我只升级了nginx,查看版本没有问题,但是执行 nginx -s reload 时报错了:
[[email protected]:~]$ nginx -s reload

nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1014001 in /usr/share/nginx/modules/mod-http-geoip.conf:1

原来是升级过后模块不一样了,需要协助之前的模块,并安装新的模块:

# 查看旧的模块
yum list installed | grep nginx-mod*

nginx-mod-http-geoip.x86_64           1:1.12.2-2.el7              @epel
nginx-mod-http-image-filter.x86_64    1:1.12.2-2.el7              @epel
nginx-mod-http-perl.x86_64            1:1.12.2-2.el7              @epel
nginx-mod-http-xslt-filter.x86_64     1:1.12.2-2.el7              @epel
nginx-mod-mail.x86_64                 1:1.12.2-2.el7              @epel
nginx-mod-stream.x86_64               1:1.12.2-2.el7              @epel

# 卸载旧的模块
yum -y remove nginx-mod*

# 安装新的模块
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-geoip-1.14.1-1.el7_4.ngx.x86_64.rpm
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-image-filter-1.14.1-1.el7_4.ngx.x86_64.rpm
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-perl-1.14.1-1.el7_4.ngx.x86_64.rpm
yum -y install https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-module-xslt-1.14.1-1.el7_4.ngx.x86_64.rpm

查看 nginx 官方的 rpm 仓库,我们可以发现,nginx-mod-mail 和 nginx-mod-stream 这两个模块不存在了,其实是他们已经被集成到了 nginx 中了,所以上面只需要安装 4个相应的模块就够了。

参考文档

https://nopistash.wordpress.com/2018/05/03/centos7-nginx-conflicting-module-versions

nginx 官方提供了 rpm 包,我们可以直接使用 yum upgrade 来升级。

rpm 地址: https://nginx.org/packages/centos/7/x86_64/RPMS