nginx优化之隐藏版本号

某些时候为了安全,我们不希望用户能够获取到我们使用的nginx版本号,这时候就可以通过修改配置文件来显示隐藏版本号。

此方法只能隐藏版本号,要修改版本号和显示名称需要修改源码包重新编译安装。

 

隐藏版本号

修改nginx配置文件,在server标签下添加一行就好

vim nginx.conf 

server_tokens off;

 

平滑重启后生效

 /apps/nginx/sbin/nginx  -s  reload

[root@web1 ~]# curl -I 10.0.0.20
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 26 Mar 2018 13:44:15 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 25 Mar 2018 14:35:02 GMT
Connection: keep-alive
ETag: "5ab7b396-264"
Accept-Ranges: bytes

修改web服务名称

隐藏版本号,需要修改源码文件,重新编译安装

1:

编辑文件 ~nginx-1.12.2/src/core/nginx.h

找到这么几行

 

#define nginx_version 1012002
#define NGINX_VERSION "1.12.2"                         --版本号
#define NGINX_VER "nginx/" NGINX_VERSION      --名称

将服务器修改为YWS

#define NGINX_VER "YWS/" NGINX_VERSION

2:

其实修改上面一个大部分就会生效!!!

 

 src/http/ngx_http_header_filter_module.c (HTTP ResponseHeader)

static char ngx_http_server_string[] = "Server: nginx" CRLF;

修改为:

static char ngx_http_server_string[] = "Server: YWS" CRLF;

 

3: src/http/ngx_http_special_response.c 

static u_char ngx_http_error_tail[] =
"<hr><center>nginx</center>" CRLF
"</body>" CRLF
"</html>" CRLF

修改为:

static u_char ngx_http_error_tail[] =
"<hr><center>YWS</center>" CRLF
"</body>" CRLF
"</html>" CRLF

 

  

/usr/local/nginx/sbin/nginx -V  查看以前的编译参数

重新编译安装

 

测试一下

[root@web1 nginx-1.12.2]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: YWS/1.12.2
Date: Tue, 12 Jun 2018 02:51:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Jun 2018 02:45:45 GMT
Connection: keep-alive
ETag: "5b1f33d9-264"
Accept-Ranges: bytes

 

 

posted @ 2018-03-26 21:53  sunny18  阅读(309)  评论(0编辑  收藏  举报