Nginx隱藏主機信息,proxy_hide_header 與fastcgi_hide_header


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 的下載地址請點這里

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM