詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用
ngx_http_headers_module模塊
一. 前言
ngx_http_headers_module模塊提供了兩個重要的指令add_header和expires,來添加 “Expires” 和 “Cache-Control” 頭字段,對響應(yīng)頭添加任何域字段。add_header可以用來標(biāo)示請求訪問到哪臺服務(wù)器上,這個也可以通過nginx模塊nginx-http-footer-filter研究使用來實(shí)現(xiàn)。expires指令用來對瀏覽器本地緩存的控制。
二. add_header指令
語法: add_header name value;
默認(rèn)值: —
配置段: http, server, location, if in location
對響應(yīng)代碼為200,201,204,206,301,302,303,304,或307的響應(yīng)報文頭字段添加任意域。如:
add_header From jb51.net
三. expires指令
語法: expires [modified] time;
expires epoch | max | off;
默認(rèn)值: expires off;
配置段: http, server, location, if in location
在對響應(yīng)代碼為200,201,204,206,301,302,303,304,或307頭部中是否開啟對“Expires”和“Cache-Control”的增加和修改操作。
可以指定一個正或負(fù)的時間值,Expires頭中的時間根據(jù)目前時間和指令中指定的時間的和來獲得。
epoch表示自1970年一月一日00:00:01 GMT的絕對時間,max指定Expires的值為2037年12月31日23:59:59,Cache-Control的值為10 years。
Cache-Control頭的內(nèi)容隨預(yù)設(shè)的時間標(biāo)識指定:
·設(shè)置為負(fù)數(shù)的時間值:Cache-Control: no-cache。
·設(shè)置為正數(shù)或0的時間值:Cache-Control: max-age = #,這里#的單位為秒,在指令中指定。
參數(shù)off禁止修改應(yīng)答頭中的"Expires"和"Cache-Control"。
實(shí)例一:對圖片,flash文件在瀏覽器本地緩存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
實(shí)例二:對js,css文件在瀏覽器本地緩存1小時
location ~ .*\.(js|css)$
{
expires 1h;
}
ngx_headers_more模塊
一. 介紹ngx_headers_more
ngx_headers_more 用于添加、設(shè)置和清除輸入和輸出的頭信息。nginx源碼沒有包含該模塊,需要另行添加。
該模塊是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)容類型,通過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選項(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包含該模塊。
三. 指令說明
more_set_headers
語法:more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段:輸出報頭過濾器
替換(如有)或增加(如果不是所有)指定的輸出頭時響應(yīng)狀態(tài)代碼與-s選項(xiàng)相匹配和響應(yīng)的內(nèi)容類型的-t選項(xiàng)指定的類型相匹配的。
如果沒有指定-s或-t,或有一個空表值,無需匹配。因此,對于下面的指定,任何狀態(tài)碼和任何內(nèi)容類型都講設(shè)置。
more_set_headers "Server: my_server";
具有相同名稱的響應(yīng)頭總是覆蓋。如果要添加頭,可以使用標(biāo)準(zhǔn)的add_header指令代替。
單個指令可以設(shè)置/添加多個輸出頭。如:
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
最后兩個有效清除的頭名稱的值。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 <content-type list>]... [-s <status-code list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段:輸出報頭過濾器
清除指定的輸出頭。
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-*';
清除開始由“X-Hidden-”任何輸出頭。
more_set_input_headers
語法:more_set_input_headers [-r] [-t <content-type list>]... <new-header>...
默認(rèn)值:no
配置段:http, server, location, location if
階段: rewrite tail
非常類似more_set_headers,不同的是它工作在輸入頭(或請求頭),它僅支持-t選項(xiàng)。
注意:使用-t選項(xiàng)的是過濾請求頭的Content-Type,而不是響應(yīng)頭的。
more_clear_input_headers
語法: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)頭模塊,該模塊不會對下面頭有效: Expires, Cache-Control, 和Last-Modified。
2. 使用此模塊無法刪除Connection的響應(yīng)報頭。唯一方法是更改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ù)器是什么軟件,什么版本,同時隱藏Centent-Type、Accept-Range、Content-Length頭信息。

more_set_headers "Server: jb51.net Web Server"; more_clear_headers "Content-Type:"; more_clear_headers "Accept-Ranges: "; more_clear_headers "Content-Length: ";

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: ";

相關(guān)文章
Nginx?Proxy?Manager配置Web?WAF應(yīng)用防火墻
Nginx?Proxy?Manager是一款功能強(qiáng)大的開源軟件,配置Web應(yīng)用防火墻,可以防止常見的web攻擊,本文就來介紹一下Nginx?Proxy?Manager配置Web?WAF應(yīng)用防火墻,感興趣的可以了解一下2025-02-02
Linux部署Nginx實(shí)現(xiàn)反向代理的方法步驟
Nginx 是一種常用、輕型且快速的 Web 服務(wù)器, 它可以在 Linux 和 Windows 上運(yùn)行,并且可以配置為反向代理服務(wù)器,本文主要介紹了Linux部署Nginx實(shí)現(xiàn)反向代理的方法步驟,感興趣的可以了解一下2023-08-08
nginx部署前端項(xiàng)目location時root和alias配置指南
nginx指定文件路徑有兩種方式root和alias,下面這篇文章主要給大家介紹了關(guān)于nginx部署前端項(xiàng)目location時root和alias配置的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
修改nginx站點(diǎn)根目錄總結(jié)經(jīng)驗(yàn)(小結(jié))
這篇文章主要介紹了修改nginx站點(diǎn)根目錄總結(jié)經(jīng)驗(yàn)(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Nginx啟動成功卻無法訪問網(wǎng)頁的問題分析和解決方案(完整的排除方案)
我是用的阿里云的服務(wù)器,所以我的問題就在于阿里云服務(wù)器必須單獨(dú)開端口,在找到這個問題之前,我已經(jīng)把所有能試的方法試過了一遍都沒有問題,在增加端口之后直接成功了,如果你也遇到了這樣的問題,就和我一起排除吧2023-12-12
https如何通過nginx完成雙向認(rèn)證轉(zhuǎn)發(fā)
文章詳細(xì)介紹了HTTPS單向認(rèn)證和雙向認(rèn)證的概念,并提供了生成自簽證書、配置Nginx進(jìn)行雙向認(rèn)證的具體步驟,通過雙向認(rèn)證,服務(wù)端和客戶端可以互相驗(yàn)證身份,提升安全性,在測試過程中,使用瀏覽器訪問HTTPS接口時,需要安裝客戶端證書才能成功獲取數(shù)據(jù)2024-11-11

