Nginx服務(wù)器中HTTPHeaders的模塊怎么配置使用

這篇文章主要介紹了Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、靖江網(wǎng)站維護(hù)、網(wǎng)站推廣。

一. 前言
ngx_http_headers_module模塊提供了兩個(gè)重要的指令add_header和expires,來(lái)添加 “expires” 和 “cache-control” 頭字段,對(duì)響應(yīng)頭添加任何域字段。add_header可以用來(lái)標(biāo)示請(qǐng)求訪問(wèn)到哪臺(tái)服務(wù)器上,這個(gè)也可以通過(guò)nginx模塊nginx-http-footer-filter研究使用來(lái)實(shí)現(xiàn)。expires指令用來(lái)對(duì)瀏覽器本地緩存的控制。
二. add_header指令
語(yǔ)法: add_header name value;
默認(rèn)值: —
配置段: http, server, location, if in location
對(duì)響應(yīng)代碼為200,201,204,206,301,302,303,304,或307的響應(yīng)報(bào)文頭字段添加任意域。如:

add_header from jb51.net
三. expires指令
語(yǔ)法: expires [modified] time;
expires epoch | max | off;
默認(rèn)值: expires off;
配置段: http, server, location, if in location
在對(duì)響應(yīng)代碼為200,201,204,206,301,302,303,304,或307頭部中是否開(kāi)啟對(duì)“expires”和“cache-control”的增加和修改操作。
可以指定一個(gè)正或負(fù)的時(shí)間值,expires頭中的時(shí)間根據(jù)目前時(shí)間和指令中指定的時(shí)間的和來(lái)獲得。
epoch表示自1970年一月一日00:00:01 gmt的絕對(duì)時(shí)間,max指定expires的值為2037年12月31日23:59:59,cache-control的值為10 years。
cache-control頭的內(nèi)容隨預(yù)設(shè)的時(shí)間標(biāo)識(shí)指定:
·設(shè)置為負(fù)數(shù)的時(shí)間值:cache-control: no-cache。
·設(shè)置為正數(shù)或0的時(shí)間值:cache-control: max-age = #,這里#的單位為秒,在指令中指定。
參數(shù)off禁止修改應(yīng)答頭中的"expires"和"cache-control"。
實(shí)例一:對(duì)圖片,flash文件在瀏覽器本地緩存30天

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 {
      expires 30d;
 }

實(shí)例二:對(duì)js,css文件在瀏覽器本地緩存1小時(shí)

location ~ .*\.(js|css)$
 {
      expires 1h;
 }

ngx_headers_more模塊
一. 介紹ngx_headers_more
ngx_headers_more 用于添加、設(shè)置和清除輸入和輸出的頭信息。nginx源碼沒(méi)有包含該模塊,需要另行添加。
該模塊是ngx_http_headers_module模塊的增強(qiáng)版,提供了更多的實(shí)用工具,比如復(fù)位或清除內(nèi)置頭信息,如content-type, content-length, 和server。
可以允許你使用-s選項(xiàng)指定http狀態(tài)碼,使用-t選項(xiàng)指定內(nèi)容類型,通過(guò)more_set_headers 和 more_clear_headers 指令來(lái)修改輸出頭信息。如:

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選項(xiàng)也可以在more_set_input_headers和more_clear_input_headers指令中使用。
不像標(biāo)準(zhǔn)頭模塊,該模塊的指示適用于所有的狀態(tài)碼,包括4xx和5xx的。 add_header只適用于200,201,204,206,301,302,303,304,或307。

二. 安裝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包含該模塊。
三. 指令說(shuō)明
more_set_headers
語(yǔ)法:more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段:輸出報(bào)頭過(guò)濾器
替換(如有)或增加(如果不是所有)指定的輸出頭時(shí)響應(yīng)狀態(tài)代碼與-s選項(xiàng)相匹配和響應(yīng)的內(nèi)容類型的-t選項(xiàng)指定的類型相匹配的。
如果沒(méi)有指定-s或-t,或有一個(gè)空表值,無(wú)需匹配。因此,對(duì)于下面的指定,任何狀態(tài)碼和任何內(nèi)容類型都講設(shè)置。

more_set_headers  "server: my_server";

具有相同名稱的響應(yīng)頭總是覆蓋。如果要添加頭,可以使用標(biāo)準(zhǔn)的add_header指令代替。
單個(gè)指令可以設(shè)置/添加多個(gè)輸出頭。如:

more_set_headers 'foo: bar' 'baz: bah';

在單一指令中,選項(xiàng)可以多次出現(xiàn),如:

 more_set_headers -s 404 -s '500 503' 'foo: bar';

等同于:

 more_set_headers -s '404 500 503' 'foo: bar';

新的頭是下面形式之一:

  • name: value

  • name:

  • name

最后兩個(gè)有效清除的頭名稱的值。nginx的變量允許是頭值,如:

set $my_var "dog";
more_set_headers "server: $my_var";

注意:more_set_headers允許在location的if塊中,但不允許在server的if塊中。下面的配置就報(bào)語(yǔ)法錯(cuò)誤:

 # this is not allowed!
 server {
    if ($args ~ 'download') {
      more_set_headers 'foo: bar';
    }
    ...
  }
more_clear_headers

語(yǔ)法:more_clear_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段:輸出報(bào)頭過(guò)濾器
清除指定的輸出頭。

more_clear_headers -s 404 -t 'text/plain' foo baz;

等同于

more_set_headers -s 404 -t 'text/plain' "foo: " "baz: ";

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-*';

清除開(kāi)始由“x-hidden-”任何輸出頭。
more_set_input_headers
語(yǔ)法:more_set_input_headers [-r] [-t <content-type list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段: rewrite tail
非常類似more_set_headers,不同的是它工作在輸入頭(或請(qǐng)求頭),它僅支持-t選項(xiàng)。
注意:使用-t選項(xiàng)的是過(guò)濾請(qǐng)求頭的content-type,而不是響應(yīng)頭的。
more_clear_input_headers
語(yǔ)法:more_clear_input_headers [-t <content-type list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段: rewrite tail
清除指定輸入頭。如:

more_clear_input_headers -s 404 -t 'text/plain' foo baz;

等同于

more_set_input_headers -s 404 -t 'text/plain' "foo: " "baz: ";

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. 不同于標(biāo)準(zhǔn)頭模塊,該模塊不會(huì)對(duì)下面頭有效: expires, cache-control, 和last-modified。
2. 使用此模塊無(wú)法刪除connection的響應(yīng)報(bào)頭。唯一方法是更改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
location /foo {
  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';

六. 應(yīng)用ngx_headers_more
修改web服務(wù)器是什么軟件,什么版本,同時(shí)隱藏centent-type、accept-range、content-length頭信息。

Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用

more_set_headers "server: jb51.net web server";
more_clear_headers "content-type:";
more_clear_headers "accept-ranges: ";
more_clear_headers "content-length: ";

Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用

404狀態(tài)碼添加header
配置如下:

more_set_headers "server: jb51.net web server";
more_set_headers -s 404 "error: not found";
more_clear_headers "content-type:";
more_clear_headers "accept-ranges: ";
more_clear_headers "content-length: ";

Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用

關(guān)于“Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Nginx服務(wù)器中HTTP Headers的模塊怎么配置使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文標(biāo)題:Nginx服務(wù)器中HTTPHeaders的模塊怎么配置使用
新聞來(lái)源:http://bm7419.com/article2/psddic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、域名注冊(cè)、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈、App開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司