nginx升级步骤,nginx版本升级步骤
最近nginx 爆了很多 漏洞,很有可可能影响线上业务运行,解决方案是升级nginx 到最新版本。下面我将升级nginx步骤梳理写一下
升级,也就是说线上有运行的nginx,我么可以线看一下当前版本,以及安装信息。
1. 查看老版本以及配置安装信息
# 查看版本 和 安装的扩展内容
nginx -V
nginx version: nginx/1.31.3
built by gcc 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04.1)
built with OpenSSL 3.5.6 7 Apr 2026
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_realip_module --with-openssl=/usr/local/openssl-3.5.6 --with-ld-opt='-L/usr/local/openssl-3.5.6/lib -Wl,-rpath,/usr/local/openssl-3.5.6/lib'
2. 下载新版本nginx ,官网地址 https://nginx.org/en/download.html
#下载
wget https://nginx.org/download/nginx-1.31.4.tar.gz
3. 下载后,生成新的nginx ,注意这里要区分一下,你是否有安装的第三方依赖
比如上面安装了/usr/local/src/ModSecurity-nginx 。 有的第三方是依赖 新版本,有的是根据nginx版本使用不同的版本。 我这里是使用了公共版本,将生成新的so文件。
# (1)配置make文件,到下载的nginx 文件解压目录中
tar -xvf nginx-1.31.2.tar
cd nginx-1.31.2
# 使用第一步的 configure arguments: 参数 直接复制过来,如果有新的,自行调整。
./configure --user=www --group=www --prefix=/usr/local/nginx --with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_realip_module --with-openssl=/usr/local/openssl-3.5.6 --with-ld-opt='-L/usr/local/openssl-3.5.6/lib -Wl,-rpath,/usr/local/openssl-3.5.6/lib'
# 完毕后 make ,只make ,切记不要 make install
make
# 完毕后,会有objs 文件夹,以及如果有扩展的新的.so 文件
# (2) 替换原来的nginx
cd /usr/local/nginx/sbin
mv nginx nginx.old
cp /root/nginx-1.31.2/objs/nginx ./nginx
# 测试 配置
./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# (3)测试成功后,重启nginx,根据自己的安装情况
kill -HUP $(cat /usr/local/nginx/logs/nginx.pid.oldbin) # 恢复旧 master
kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) # 关掉新 master
mv /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx
4. 异常回滚,直接将原来的nginx 重命名回来即可。
cd /usr/local/nginx/sbin
mv nginx.old nginx
然后重启