Nginx安装后默认直接显示自身的版本号,出于安全或隐私的考虑,需要隐藏或者修改Nginx的版本信息。
1、隐藏Nginx的版本信息非常简单,只需要修改nginx配置文件即可实现,且不会对正在运行中的程序造成影响。
先查看修改前的显示信息:
curl --head http://www.xxxx.com
HTTP/1.1 200 OK
Server: nginx/1.4.4
Date: Fri, 19 Dec 2014 03:39:08 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.7
Vary: Accept-Encoding
修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
添加或者修改 http 段中server_tokens 的参数
server_tokens on;
重启nginx服务(有nginx启动脚本,详见《Nginx启动脚本》)
Service nginx restart
先查看修改后的显示信息:
curl --head http://www.xxxx.com
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 19 Dec 2014 03:38:16 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.7
Vary: Accept-Encoding
2、修改nginx显示信息, 修改版本号的要比隐藏版本号的方法复杂,它需要在安装nginx之前进行。
解压nginx的源码包并进入src目录
vim /usr/local/src/nginx-1.4.4/src/core/nginx.h
进行修改:
#define nginx_version 1004004
#define NGINX_VERSION "1.4.4"
#define NGINX_VER "nginx/" NGINX_VERSION
#define NGINX_VAR "NGINX"
修改完为:
#define nginx_version 1004004
#define NGINX_VERSION "1.4.4"
#define NGINX_VER "nginx1/" NGINX_VERSION
#define NGINX_VAR "NGINX1"
vim /usr/local/src/nginx-1.4.4/src/http/ngx_http_header_filter_module.c
找到static char ngx_http_server_string[] = "Server: nginx" CRLF; 这一行,
修改里面的Server:nginx为你想要的名称,如:Server:nginx1
编译安装(详见《LNMP环境搭建》)
2014-12-22 09:32