一、方案一:YUM 官方源平滑升级(推荐,最省事)【此方案最高升到1.26的版本,稳定版(stable)源最高只到 1.26.x】
1. 准备工作(必做)
# 1. 备份配置与二进制(关键)
cp -a /etc/nginx /etc/nginx.bak.$(date +%F)
cp /usr/sbin/nginx /usr/sbin/nginx.old.$(date +%F)
# 2. 配置 Nginx 官方 YUM 源(默认源版本旧)
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF
# 3. 清理缓存并查看可升级版本
yum clean all && yum makecache fast
yum list updates nginx
2. 升级(自动保留配置)
yum update nginx -y
如果升级时失败使用如下
yum update nginx -y --nogpgcheck
万一升级出问题,一键回滚
# 下载 1.21.1 主线版
wget https://nginx.org/packages/mainline/centos/7/x86_64/RPMS/nginx-1.21.1-1.el7.ngx.x86_64.rpm
# 强制降级覆盖
rpm -Uvh --oldpackage nginx-1.21.1-1.el7.ngx.x86_64.rpm
# 验证
nginx -v
3. 验证与平滑重载(不中断)
# 1. 检查新版本
nginx -v
nginx -V
# 2. 测试配置(必须通过)
nginx -t
# 3. 平滑重载(零停机)
systemctl reload nginx
# 或用 nginx 原生命令
nginx -s reload
# 4. 验证服务正常
systemctl status nginx
curl -I http://localhost
4. 常见问题处理
- 模块冲突(如
ngx_http_geoip_module.so冲突)yum remove nginx-mod* -y yum install nginx-module-* -y systemctl reload nginx - 配置语法错误
nginx -t # 定位错误 # 修复后再 reload
切换到 Nginx 主线版 YUM 源【主线版(mainline)源最高到1.27】
cat > /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=1 # 启用主线版源
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
二、RPM升级,可直接 RPM 升级,保留配置、无依赖问题
# 备份(必做)
cp -a /etc/nginx /etc/nginx.bak.$(date +%F)
cp /usr/sbin/nginx /usr/sbin/nginx.old
# 下载官方 1.27.0 RPM
wget https://nginx.org/packages/mainline/centos/7/x86_64/RPMS/nginx-1.27.0-2.el7.ngx.x86_64.rpm
# 平滑升级
rpm -Uvh --replacepkgs nginx-1.27.2-1.el7.ngx.x86_64.rpm
# 验证与重载
nginx -v
nginx -t
systemctl reload nginx
三、若必须上 1.28.2,只能源码编译(官方不再为 el7 提供 1.28+ RPM)
# 依赖
yum install -y gcc make pcre-devel zlib-devel openssl-devel
# 下载源码
cd /usr/local/src
wget https://nginx.org/download/nginx-1.28.2.tar.gz
tar zxf nginx-1.28.2.tar.gz
cd nginx-1.28.2
# 配置(复制你当前 nginx -V 的参数)
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--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 \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module
# 编译
make -j$(nproc)
# 替换二进制
cp -f objs/nginx /usr/sbin/nginx
# 检查配置是否正常
nginx -t
# 平滑启动新 master 进程
kill -USR2 $(cat /var/run/nginx.pid)
# 优雅关闭旧 worker 进程(处理完现有请求再退出)
kill -WINCH $(cat /var/run/nginx.pid.oldbin)
# 确认服务正常后,关闭旧 master
kill -QUIT $(cat /var/run/nginx.pid.oldbin)
安全审计中有时会有
漏洞名称 HTTP服务器版本信息泄漏
漏洞描述 目标服务器返回的信息头中包含了Web Server的软件或者版本信息。
可以安装 nginx的headers-more-nginx-module模块修改或隐藏响应头信息
一、安装
1.下载 headers-more-nginx-module
下载地址
https://github.com/openresty/headers-more-nginx-module/tags
wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.37.tar.gz
2.解压
tar -zxvf v0.37.tar.gz
解压后会多出一个headers-more-nginx-module-0.37文件夹
3.查看当前系统nginx信息
// 查看当前nginx加载的模块,这里使用的是yum安装的
nginx -V
接下来操作的是yum安装的,如何编译第三方模块
4.下载对应版本的nginx,这里的版本必须和之前yum安装的NGINX版本一致
# 下载
wget http://nginx.org/download/nginx-1.26.1.tar.gz
# 解压
[root@localhost tools]# tar -zxvf nginx-1.26.1.tar.gz
5.编译
# 先备份
cp /usr/sbin/nginx /usr/sbin/nginx_old
yum install gcc pcre-devel zlib-devel make unzip openssl-devel -y
cd nginx-1.26.1
这里的编译模块是复制之前yum安装的编译模块,nginx -V
./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 --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' --add-module=/headers-more-nginx-module-0.37/
在末尾添加 --add-module=/headers-more-nginx-module-0.37
注意路径【/headers-more-nginx-module-0.37】要换成你自己的
configure完成后进行make(如原本无nginx,make后还需make install)
6.复制新的nginx到目录
cp objs/nginx /usr/sbin/nginx
7.重启nginx生效
二、测试
修改nginx配置文件,增加代码块
1、隐藏版本

加入参数:server_tokens off;

效果(版本已经看不到了):

2、隐藏Server
加入参数 : more_clear_headers 'Server';

效果(已经看不到server了)

3、伪装Server
加入参数: more_set_headers 'Server: my-server';

效果(server已经变成我们自定义的)


8208

被折叠的 条评论
为什么被折叠?



