您的位置:首页 > 运维架构 > Nginx

Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header

2016-02-04 11:01 555 查看
proxy_hide_header 与fastcgi_hide_header qunyingliu

nginx中proxy_hide_header 与fastcgi_hide_header都可以隐藏主机头信息,两者在具体使用时还是有着一定的区别的。刚好业务使用的nginx反向代理在显示响应头时将后端机器的PHP版本显示了出来,虽说在用户体验和安全上并无大的影响,但是将PHP版本信息暴漏出来总是不妥的。借此机会,可以复习下proxy_hide_header 与fastcgi_hide_header的使用。

proxy_hide_header在ngx_http_proxy_module下,fastcgi_hide_header在ngx_http_fastcgi_module模块下,作用相同的但是作用的地方有一些区别。

当nginx作为反向代理时,也就是nginx转发请求后端其他webserver(例如nginx+apache)时,当我们想要隐藏后端webserver主机信息的时候,我们使用proxy_hide_header来屏蔽后端主机信息。

当nginx作为webserver时,也就是nginx直接在服务器上提供web服务(例如nginx+php/php-fpm)处理用户请求,当我们想要隐藏webserver主机信息的时候,我们使用fastcgi_hide_header来屏蔽当前主机信息(尤其是php中相关信息)。

我们当前nginx是作为反向代理来使用,在配置proxy_hide_header前,通过浏览器我们可以看到主机响应头中包含php版本信息(X-Powered-By: PHP/5.4.43),我们的目的就是将这个显示内容从响应头中去掉。

请求头:

Host: v.l.qq.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: cm_cookie=V1,110015&1&AQEB6fOaVNNwm3PnpE9-5fO6F1GuuckO58xf&151023&151023,10008&mVXmnXsXpWqtoVVosntqqmsWsnsZYqql=&AQEBOy5mIcgjMROV-G8UDto2xjn787qAVk0u&160130&160130,110069&60ccd2e7aab778&AQEBUrmfm9YUuR9a-uIl2zxzHICDkArByOTr&160130&160130; appuser=513E20192260A681; M_D=1; psessionid=b499db0e_1454552430_0_52917; psessiontime=1454552430
Connection: keep-alive
Cache-Control: max-age=0

响应头:

Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Thu, 04 Feb 2016 02:20:36 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.43

根据官网说明,proxy_hide_header 可在http, server, location区段使用。

语法: proxy_hide_header field;
默认值: —
上下文: http, server, location
nginx默认不会将“Date”、“Server”、“X-Pad”,和“X-Accel-...”响应头发送给客户端。proxy_hide_header指令则可以设置额外的响应头,这些响应头也不会发送给客户端。相反的,如果希望允许传递某些响应头给客户端,可以使用proxy_pass_header指令。

一般nginx反向代理会配置很多站点,每个站点配置费时费力而且少有遗漏,主机信息还是会被泄露的。根据上面的说明,我们将proxy_hide_header 配置在http区段,如下所示:

http {
server_tokens off;
server_tag off;
autoindex off;
access_log off;
include mime.types;
default_type application/octet-stream;
proxy_hide_header X-Powered-By;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1000m;
client_body_buffer_size 256k;

检查nginx配置文件语法:
/usr/local/nginx/sbin/nginx -t 或/etc/init.d/nginx check
重启nginx服务:
/etc/init.d/nginx restart

配置后的主机响应头信息:

Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Thu, 04 Feb 2016 02:50:16 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding

通过查看官网信息,如下所示

syntax: fastcgi_hide_header field;
default: —
context: http, server, location
By default, nginx does not pass the header fields “Status” and “X-Accel-...” from the response of the FastCGI server to a client. The fastcgi_hide_header directive sets additional fields that will not be passed. If, on the contrary, the passing of fields needs to be permitted, the fastcgi_pass_header directive can be used.
可以看到fastcgi_hide_header 和proxy_hide_header的用途是一样的,作用在FastCGI server模式中,而不是Proxy模式下。

总结:
fastcgi_hide_header 和proxy_hide_header的都可以用来隐藏主机信息,fastcgi_hide_header 在fastcgi模式下起作用,proxy_hide_header在proxy模式下起作用。同样,我们会发现ngx_http_proxy_module和ngx_http_fastcgi_module模块中有很多作用相同的模块。

本文出自 “运维者说:从菜鸟到老鸟” 博客,请务必保留此出处http://liuqunying.blog.51cto.com/3984207/1741061
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: