Nginx隱藏主機信息,proxy_hide_header 與fastcgi_hide_header

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章