ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-06-17 12:59:30  阅读:325  来源: 互联网

标签:src nginx 版本号 sunginx apps Nginx 隐藏 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.18"
#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
……
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/apps/nginx|" \
	-e "s|%%PID_PATH%%|/apps/nginx/logs/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/apps/nginx/conf/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/apps/nginx/logs/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/usr/local/src/nginx-1.20.1'
nginx version: sunginx/sunginx/

平滑升级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-1.20.1]# nginx -v
nginx version: sunginx/sunginx/
[root@C8-189 nginx-1.20.1]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server:sunginx
Date: Thu, 17 Jun 2021 12:35:30 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

响应报文头

标签:src,nginx,版本号,sunginx,apps,Nginx,隐藏,NGINX
来源: https://blog.csdn.net/timonium/article/details/117985781

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有