1、隐藏版本号
在http下添加server_tokens字段,存在三种值,分别为on、off、build,对应源码如下
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
if (r->headers_out.server == NULL) {
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
p = ngx_http_server_full_string;
len = sizeof(ngx_http_server_full_string) - 1;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
p = ngx_http_server_build_string;
len = sizeof(ngx_http_server_build_string) - 1;
} else {
p = ngx_http_server_string;
len = sizeof(ngx_http_server_string) - 1;
}
b->last = ngx_cpymem(b->last, p, len);
}
on时为版本号,build时返回build号,off时返回自定义字符串(但是server仍然存在)
2、删除server响应头
如果需要彻底删除响应头,比较彻底方法是重新编译nginx,方法如下
1、下载所需版本源码,然后修改源码,注释掉server返回
if (r->headers_out.server == NULL) {
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
p = ngx_http_server_full_string;
len = sizeof(ngx_http_server_full_string) - 1;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
p = ngx_http_server_build_string;
len = sizeof(ngx_http_server_build_string) - 1;
} else {
p = ngx_http_server_string;
len = sizeof(ngx_http_server_string) - 1;
}
//b->last = ngx_cpymem(b->last, p, len);
}
重新编译,可能需要安装openssl等内容,命令如下
yum install -y openssl openssl-devel
在Nginx源码目录中运行./configure
脚本,可以加上新的参数
./configure --with-http_ssl_module
最后make即可,生成二进制文件一般存在于objs目录下