【Docker容器中的nginx平滑升级】

nginx平滑升级

博主所在公司最近在扫描漏洞,很不幸,收到一个nginx安全漏洞,故写一下我是如何解决的吧,如果你不是部署在docker里也可以通用的。

漏洞描述

nginx安全漏洞
看起来很简单,我们只需要升级一下nginx版本,因为这个是版本带来的BUG

开始升级

进入安装有nginx的docker容器
[root@localhost ~]# docker exec -it nginx bash
先看下当前nginx版本,很明显版本过低了,在0.6.18-1.20.0之间,升级它
[root@b656bb053512 /]# nginx -v
nginx version: nginx/1.16.0
备份当前nginx二进制文件,注意自己安装路径,我的是自己编译安装自己指定路径
[root@b656bb053512 /]# ll /usr/sbin/nginx
-rwxr-xr-x. 1 root root 9307840 Dec  7 09:08 /usr/sbin/nginx
[root@b656bb053512 /]# mv /usr/sbin/nginx /usr/sbin/nginx.bk
[root@b656bb053512 /]# cd /usr/sbin/
[root@b656bb053512 sbin]# ll
-rwxr-xr-x. 1 root root 9307840 Dec  7 09:08 /usr/sbin/nginx.bk
使用wget http://nginx.org/download/nginx-1.20.2.tar.gz把最新稳定版本下载下来,或者自己下载后传到服务器再使用docker cp到容器
[root@b656bb053512 /]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
--2021-12-07 17:21:01--  http://nginx.org/download/nginx-1.20.2.tar.gz
正在解析主机 nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|52.58.199.22|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1062124 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.20.2.tar.gz”

100%[=======================================================================================================================================================================================================>] 1,062,124    766KB/s 用时 1.4s   
2021-12-07 17:21:03 (766 KB/s) - 已保存 “nginx-1.20.2.tar.gz” [1062124/1062124])
解压
[root@b656bb053512 /]# tar xf nginx-1.20.2.tar.gz 
进入 nginx-1.20.2
[root@b656bb053512 /]# cd nginx-1.20.2
[root@b656bb053512 nginx-1.20.2]# ll
total 792
-rw-r--r--. 1 1001 1001 312251 Nov 16 14:44 CHANGES
-rw-r--r--. 1 1001 1001 476577 Nov 16 14:44 CHANGES.ru
-rw-r--r--. 1 1001 1001   1397 Nov 16 14:44 LICENSE
-rw-r--r--. 1 1001 1001     49 Nov 16 14:44 README
drwxr-xr-x. 6 1001 1001   4096 Dec  7 09:22 auto
drwxr-xr-x. 2 1001 1001    168 Dec  7 09:22 conf
-rwxr-xr-x. 1 1001 1001   2590 Nov 16 14:44 configure
drwxr-xr-x. 4 1001 1001     72 Dec  7 09:22 contrib
drwxr-xr-x. 2 1001 1001     40 Dec  7 09:22 html
drwxr-xr-x. 2 1001 1001     21 Dec  7 09:22 man
drwxr-xr-x. 9 1001 1001     91 Dec  7 09:22 src
注意# 编译选项与旧版本的编译选项要一直,可以先用nginx -V 查看编译选项
[root@b656bb053512 nginx-1.20.2]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (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
[root@b656bb053512 nginx-1.20.2]# 
根据之前版本的编译选项,我们配置一下
[root@d1af8b62b986 nginx-1.20.2]# ./configure --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
checking for OS
 + Linux 3.10.0-1127.13.1.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
......
......
......
接下来make一下,这里只make 不需要make install
[root@d1af8b62b986 nginx-1.20.2]# make
make -f objs/Makefile
make[1]: Entering directory `/nginx-1.20.2'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \
	src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_palloc.o \
	src/core/ngx_palloc.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_array.o \
	src/core/ngx_array.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_list.o \
	src/core/ngx_list.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_hash.o \
......
......
......
上述完成以后,会在当前我解压缩nginx的objs下面产生一个nginx二进制文件
[root@b656bb053512 nginx-1.20.2]# cd objs/
[root@b656bb053512 objs]# ll
total 9308
-rw-r--r--. 1 root root   62235 Dec  7 09:04 Makefile
-rw-r--r--. 1 root root   19458 Dec  7 09:04 autoconf.err
-rwxr-xr-x. 1 root root 9307840 Dec  7 09:05 nginx
-rw-r--r--. 1 root root    5500 Dec  7 09:05 nginx.8
-rw-r--r--. 1 root root    9559 Dec  7 09:04 ngx_auto_config.h
-rw-r--r--. 1 root root     657 Dec  7 09:04 ngx_auto_headers.h
-rw-r--r--. 1 root root   10901 Dec  7 09:04 ngx_modules.c
-rw-r--r--. 1 root root   96976 Dec  7 09:05 ngx_modules.o
drwxr-xr-x. 9 root root      91 Dec  7 09:04 src
[root@b656bb053512 objs]# 
复制新版本的nginx二进制文件到就二进制文件路径,覆盖它,如果cp不行可用mv覆盖
[root@b656bb053512 objs]# cp nginx /usr/sbin/
[root@b656bb053512 objs]# nginx -v
nginx version: nginx/1.20.2
重启docker容器,重新启动nginx就可以了
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值