|
隐藏Nginx软件版本号
隐藏Nginx版本号
1、调整参数隐藏版本号信息
在Nginx配置文件nginx.conf中的http标签段内加入“server_tokens off;”参数如下:
1
2
3
4
5
6
| http
{
……
server_tokens off;
……
}
|
此参数放置在http标签内,作用是控制http response header内的web服务版本信息的显示,以及错误信息中web服务版本信息的显示。
server_tokens参数说明:
1
2
3
4
5
6
7
| syntax: server_tokens on|off;
#<=此行为参数语法,on为开启,off为关闭
default: server_tokens on;
#<=此行意思是不配置该参数,软件默认情况的结果
context: http,server,location
#<=此行为server_tokens参数可以放置的位置
参数作用:激活或禁止nginx的版本信息显示在报错信息和server的响应首部位置中
|
2、更改源码隐藏版本号
第一步依次修改3个Nginx源码文件
(1)修改第一个文件nginx-1.6.3/src/core/nginx.h如下:
1
2
3
4
5
| [iyunv@nginx01 core]# sed -n '13,17p' nginx.h
#define NGINX_VERSION "1.6.3" #<=修改为想要显示的版本号
#define NGINX_VER "nginx/" NGINX_VERSION #<=将nginx修改为想要修改的软件名
#define NGINX_VAR "NGINX" #<=将nginx修改为想要修改的软件名称
#define NGX_OLDPID_EXT ".oldbin"
|
修改后的结果为:
1
2
3
4
5
| [iyunv@nginx01 core]# sed -n '13,17p' nginx.h
#define NGINX_VERSION "2.2.23"
#define NGINX_VER "OWS/" NGINX_VERSION
#define NGINX_VAR "OWS"
#define NGX_OLDPID_EXT ".oldbin"
|
(2)修改第二个文件nginx-1.6.3/src/http/ngx_http_header_filter_module.c的第49行:
1
2
| [iyunv@nginx01 http]# grep -n 'Server: nginx' ngx_http_header_filter_module.c
49:static char ngx_http_server_string[] = "Server: nginx" CRLF;
|
替换后的结果:
1
2
3
| [iyunv@nginx01 http]# sed -i 's#Server: nginx#Server: OWS#g' ngx_http_header_filter_module.c
[iyunv@nginx01 http]# grep -n 'Server: OWS' ngx_http_header_filter_module.c
49:static char ngx_http_server_string[] = "Server: OWS" CRLF;
|
(3)修改第三个文件nginx-1.6.3/src/http/ngx_http_special_response.c
1
2
3
4
5
6
7
8
9
| [iyunv@nginx01 http]# sed -n '21,30p' ngx_http_special_response.c
static u_char ngx_http_error_full_tail[] =
"<hr><center>" NGINX_VER "</center>" CRLF #<=此行需要修改
"</body>" CRLF
"</html>" CRLF
;
static u_char ngx_http_error_tail[] =
"<hr><center>nginx</center>" CRLF #<=此行需要修改
"</body>" CRLF
|
修改后的结果为:
1
2
3
4
5
6
7
8
9
10
| [iyunv@nginx01 http]# sed -n '21,30p' ngx_http_special_response.c
static u_char ngx_http_error_full_tail[] =
"<hr><center>" NGINX_VER "(http://zhanghongxin.blog.iyunv.com)</center>" CRLF
#<=此行是定义对外展示的内容
"</body>" CRLF
"</html>" CRLF
;
static u_char ngx_http_error_tail[] =
"<hr><center>OWS</center>" CRLF #<=此行将对外展示的nginx名称更改为OWS
"</body>" CRLF
|
第二步修改后编译软件,使其生效
修改后再编译安装软件,如果是已安装好的服务,需要重新编译Nginx,配好配置,启动服务。
更改Nginx服务的默认用户
查看nginx对应的默认用户:
1
2
| [iyunv@nginx01 conf]# grep '#user' nginx.conf.default
#user nobody;
|
(1)为Nginx服务建立新用户
1
| useradd -s /sbin/nologin -M nginx
|
(2)配置Nginx服务,让其使用刚建立的nginx用户
第一种直接更改配置文件参数,将默认的#user nobody;改为如下内容:
如果注释或不设置上述参数,默认为nobody用户,不推荐使用nobody用户,最好采用一个普通用户。
第二种为直接在编译Nginx软件时指定编译的用户和组,命令如下:
1
2
3
4
5
6
| ./configure \
--user=nginx \
--group=nginx \
--prefix=/application/nginx1.6.3 \
--with-http_stub_status_module \
--with-http_ssl_module
|
(3)检查更改用户的效果
重新加载配置后,检查Nginx服务进程的对应用户;
1
2
3
4
5
| [iyunv@nginx01 conf]# ps -ef|grep nginx|grep -v grep
root 3895 1 0 Sep08 ? 00:00:06 nginx: master process \
/application/nginx/sbin/nginx
nginx 34007 34006 0 Sep09 ? 00:00:00 nginx: worker process
nginx 34008 34006 0 Sep09 ? 00:00:00 nginx: worker process
|
|
|