nginx隐藏版本号及nginx

该博客详细介绍了如何检查已安装的Nginx插件,停止并卸载旧版Nginx,备份配置,下载并编译新版Nginx源码,修改配置以隐藏版本号,解决编译错误,以及配置和启动新版本的Nginx。同时,还展示了如何进行系统服务配置以确保稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 查看nginx安装了哪些插件

nginx -V

# 停止并卸载老的nginx

systemctl stop nginx

ps -ef|grep nginx

# 备份老的配置

find / -name "nginx"

mv /etc/logrotate.d/nginx{,-bak}

mv /etc/nginx{,-bak}

mv /var/log/nginx

mv /var/cache/yum/x86_64/7/nginx{,-bak}

mv /var/cache/nginx{,-bak}

mv /usr/sbin/nginx{,-bak}

mv /usr/lib/debug/usr/lib64/nginx{,-bak}

mv /usr/lib/debug/usr/lib64/perl5/vendor_perl/auto/nginx{,-bak}

mv /usr/lib64/perl5/vendor_perl/auto/nginx{,-bak}

mv /usr/lib64/nginx{,-bak}

mv /usr/share/nginx{,-bak}

mv /usr/libexec/initscripts/legacy-actions/nginx{,-bak}


# mv /usr/lib/systemd/system/nginx-debug.service

# mv /usr/lib/systemd/system/nginx.service



mv /usr/share/doc/nginx-1.22.0{,-bak}

mv /usr/share/man/man8/nginx.8.gz{,-bak}

# 下载nginx源码,并解压,进入解压目录
 

wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0

修改源码配置

vi +13 src/core/nginx.h

#define NGINX_VERSION      "8.8.8"

#define NGINX_VER          "  /" NGINX_VERSION


vi +49 src/http/ngx_http_header_filter_module.c

static u_char ngx_http_server_string[] = "Server:   " CRLF;


vi +36 src/http/ngx_http_special_response.c

"<hr><center>  </center>" CRLF

# 编译新的nginx

./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'

make && make install

编译中报错修复:

yum -y install pcre-devel

# 配置新的nginx配置

mkdir -p /etc/nginx/conf.d/

cp /etc/nginx-bak/nginx.conf /etc/nginx/

cp /etc/nginx-bak/conf.d/default.conf /etc/nginx/conf.d/

cp -a /etc/nginx-bak/ssl /etc/nginx/

vi /etc/nginx/nginx.conf
# 配置文件新增隐藏版本号
http {
    server_tokens off;
}

启动测试

systemctl start nginx

cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStartPre=/bin/mkdir -p /var/run/nginx/
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

systemctl daemon-reload

systemctl restart nginx

systemctl enable nginx


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值