官網https://github.com/openresty/headers-more-nginx-module#more_clear_input_headers
一. 介紹ngx_headers_more
ngx_headers_more 用于添加、設定和清除輸入和輸出的頭資訊。nginx源碼沒有包含該子產品,需要另行添加。
該子產品是ngx_http_headers_module子產品的增強版,提供了更多的實用工具,比如複位或清除内置頭資訊,如Content-Type, Content-Length, 和Server。
可以允許你使用-s選項指定HTTP狀态碼,使用-t選項指定内容類型,通過more_set_headers 和 more_clear_headers 指令來修改輸出頭資訊。如:
more_set_headers -s 404 -t 'text/html' 'X-Foo: Bar';
輸入頭資訊也可以這麼修改,如:
location /foo {
more_set_input_headers 'Host: foo' 'User-Agent: faked';
# now $host, $http_host, $user_agent, and
# $http_user_agent all have their new values.
}
-t選項也可以在more_set_input_headers和more_clear_input_headers指令中使用。
不像标準頭子產品,該子產品的訓示适用于所有的狀态碼,包括4xx和5xx的。 add_header隻适用于200,201,204,206,301,302,303,304,或307。
标準頭子產品ngx_http_headers_module參見:《ngx_http_headers_module子產品add_header和expires指令》
二. 安裝ngx_headers_more
wget 'http://nginx.org/download/nginx-1.5.8.tar.gz'
tar -xzvf nginx-1.5.8.tar.gz
cd nginx-1.5.8/
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
--add-module=/path/to/headers-more-nginx-module
make
make install
ngx_headers_more 包下載下傳位址:http://github.com/agentzh/headers-more-nginx-module/tags
ngx_openresty包含該子產品。
三. 指令說明
more_set_headers
文法:more_set_headers [-t ]… [-s ]… …
預設值:no
配置段:http, server, location, location if
階段:輸出報頭過濾器
替換(如有)或增加(如果不是所有)指定的輸出頭時響應狀态代碼與-s選項相比對和響應的内容類型的-t選項指定的類型相比對的。
如果沒有指定-s或-t,或有一個空表值,無需比對。是以,對于下面的指定,任何狀态碼和任何内容類型都講設定。
more_set_headers "Server: my_server";
具有相同名稱的響應頭總是覆寫。如果要添加頭,可以使用标準的add_header指令代替。
單個指令可以設定/添加多個輸出頭。如:
more_set_headers 'Foo: bar' 'Baz: bah';
在單一指令中,選項可以多次出現,如:
more_set_headers -s 404 -s '500 503' 'Foo: bar';
等同于:
more_set_headers -s '404 500 503' 'Foo: bar';
新的頭是下面形式之一:
Name: Value
Name:
Name
最後兩個有效清除的頭名稱的值。Nginx的變量允許是頭值,如:
set $my_var "dog";
more_set_headers "Server: $my_var";
注意:more_set_headers允許在location的if塊中,但不允許在server的if塊中。下面的配置就報文法錯誤:
# This is NOT allowed!
server {
if ($args ~ 'download') {
more_set_headers 'Foo: Bar';
}
...
}
more_clear_headers
文法:more_clear_headers [-t ]… [-s ]… …
清除指定的輸出頭。
more_clear_headers -s 404 -t 'text/plain' Foo Baz;
等同于
more_set_headers -s 404 -t 'text/plain' "Foo: " "Baz: ";
或
more_set_headers -s 404 -t 'text/plain' Foo Baz
也可以使用通配符*,如:
more_clear_headers 'X-Hidden-*';
清除開始由“X-Hidden-”任何輸出頭。
more_set_input_headers
文法:more_set_input_headers [-r] [-t ]… …
階段: rewrite tail
非常類似more_set_headers,不同的是它工作在輸入頭(或請求頭),它僅支援-t選項。
注意:使用-t選項的是過濾請求頭的Content-Type,而不是響應頭的。
去掉請求頭
location / {
more_clear_input_headers "Accept-Encoding";
more_clear_input_headers
文法:more_clear_input_headers [-t ]… …
清除指定輸入頭。如:
more_clear_input_headers -s 404 -t 'text/plain' Foo Baz;
more_set_input_headers -s 404 -t 'text/plain' "Foo: " "Baz: ";
more_set_input_headers -s 404 -t 'text/plain' Foo Baz
四. ngx_headers_more局限性
1. 不同于标準頭子產品,該子產品不會對下面頭有效: Expires, Cache-Control, 和Last-Modified。
2. 使用此子產品無法删除Connection的響應報頭。唯一方法是更改src/ HTTP/ ngx_http_header_filter_module.c檔案。
五. 使用ngx_headers_more
# set the Server output header
more_set_headers 'Server: my-server';
# set and clear output headers
location /bar {
more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo';
more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo';
more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar';
more_clear_headers 'Content-Type';
# your proxy_pass/memcached_pass/or any other config goes here...
# set output headers
location /type {
more_set_headers 'Content-Type: text/plain';
# ...
# set input headers
set $my_host 'my dog';
more_set_input_headers 'Host: $my_host';
more_set_input_headers -t 'text/plain' 'X-Foo: bah';
# now $host and $http_host have their new values...
# replace input header X-Foo *only* if it already exists
more_set_input_headers -r 'X-Foo: howdy';
六. 應用ngx_headers_more
修改web伺服器是什麼軟體,什麼版本,同時隐藏Centent-Type、Accept-Range、Content-Length頭資訊。
more_set_headers "Server: ttlsa.com Web Server";
more_clear_headers "Content-Type:";
more_clear_headers "Accept-Ranges: ";
more_clear_headers "Content-Length: ";

404狀态碼添加header
配置如下:
more_set_headers -s 404 "Error: Not found";

參考:
<a href="http://blog.sina.com.cn/s/blog_40e9ab360102x8qq.html" target="_blank">http://blog.sina.com.cn/s/blog_40e9ab360102x8qq.html</a>
<a href="http://www.ttlsa.com/nginx/nginx-custom-header-to-return-information-module-ngx_headers_more/" target="_blank">http://www.ttlsa.com/nginx/nginx-custom-header-to-return-information-module-ngx_headers_more/</a>
<a href="https://github.com/openresty/headers-more-nginx-module#more_clear_input_headers" target="_blank">https://github.com/openresty/headers-more-nginx-module#more_clear_input_headers</a>
本文轉自Tenderrain 51CTO部落格,原文連結:http://blog.51cto.com/tenderrain/1980030,如需轉載請自行聯系原作者