阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header

80次阅读
没有评论

共计 3768 个字符,预计需要花费 10 分钟才能阅读完成。

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 模块中有很多作用相同的模块。

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/129017.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3768字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中