Nginx查看、隐藏和修改版本号

查看Nginx版本号

  • 报文头和默认的404页面会显示Nginx服务器的版本号
curl www.baidu.com -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Thu, 17 Jun 2021 03:05:55 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

百度HTTPServer

curl 10.0.0.189 -I
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 17 Jun 2021 10:40:48 GMT
Content-Type: text/html
Content-Length: 648
Last-Modified: Thu, 17 Jun 2021 10:21:41 GMT
Connection: keep-alive
ETag: "60cb2235-288"
Accept-Ranges: bytes

初装nginx响应报文头中的版本号404错误暴露版本号

Nginx关闭显示版本号

  • 关闭响应报文的Server首部显示nginx版本
  • 在nginx.conf配置文件中的http字段添加
    server_tokens off;
    添加server_tockens off字段

不再显示版本号

自定义Nginx版本信息

  • 出于开发需要,我们希望显示自定义的Nginx版本信息
  • 如果想自定义响应报文的nginx版本信息,需要修改安装文件源码文件,并重新编译安装
  • 修改 src/core/nginx.h 修改第13-14行
#define NGINX_VERSION "2021.06.10"
#define NGINX_VER "sunginx/" NGINX_VERSION
#define NGINX_VAR "SUNGINX"
  • 修改 src/http/ngx_http_header_filter_module.c第49行
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;

修改安装源码包中的源文件

sed -ri.bak '/^#define NGINX_VER/s/".*"/"sunginx\/"/' src/core/nginx.h
sed -ri.bak '/^#define NGINX_VAR/s/".*"/"SUNGINX"/' src/core/nginx.h
sed -ri.bak '/^#define NGINX_VERSION/s/".*"/"20201.06.10"/' src/core/nginx.h
sed -ri.bak '/^static u_char ngx_http_server_string\[\]/s/".*"/"Server\:sunginx"/' src/http/ngx_http_header_filter_modul

重新编译源码包

用当前nginx编译参数重新生成makefile
cd /usr/local/src/nginx-1.20.1 && nginx -V 2>&1 | awk -F: '/^configure/ {print $2}' | xargs ./configure
使用修改后的makefile进行源码编译
make && objs/nginx -v
……
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
make[1]: Leaving directory '/usr/local/src/nginx-1.20.1'
nginx version: sunginx/20201.06.10

平滑升级Nginx

备份原主程序

mv /apps/nginx/sbin/nginx{,.bak}

复制新主程序至原程序目录

cp objs/nginx /apps/nginx/sbin/

检查新主程序与配置文件是否兼容
/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
发送信号平滑升级新主程序

cat $(find / -name nginx.pid) | xargs kill -USR2
cat $(find / -name nginx.pid.oldbin) | xargs kill -winch

优雅的关闭原主程序
  • 如新主程序运行有问题,可及时拉回原主程序
    cat $(find / -name nginx.pid.oldbin) | xargs kill -hup
  • 如新程序运行没问题,则可优雅的退出原主程序
    cat $(find / -name nginx.pid.oldbin) | xargs kill -quit

Nginx自定义版本信息修改完成

[root@C8-189 ~]# nginx -v
nginx version: sunginx/20201.06.10

[root@C8-189 ~]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server:sunginx
Date: Thu, 17 Jun 2021 12:58:10 GMT
Content-Type: text/html
Content-Length: 648
Last-Modified: Thu, 17 Jun 2021 10:21:41 GMT
Connection: keep-alive
ETag: "60cb2235-288"
Accept-Ranges: bytes

响应报文头

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值