ngx_http_headers_module 模組

範例設定
指令
     add_header
     add_trailer
     expires

ngx_http_headers_module 模組允許將 “Expires” 和 “Cache-Control” 標頭欄位,以及任意欄位,新增到回應標頭中。

範例設定

expires    24h;
expires    modified +24h;
expires    @24h;
expires    0;
expires    -1;
expires    epoch;
expires    $expires;
add_header Cache-Control private;

指令

語法 add_header name value [always];
預設
上下文 http, server, location, if in location

將指定的欄位新增到回應標頭,前提是回應碼等於 200、201 (1.3.10)、204、206、301、302、303、304、307 (1.1.16, 1.0.13) 或 308 (1.13.0)。參數值可以包含變數。

可以有多個 add_header 指令。這些指令會從先前的設定層級繼承,前提是目前層級上沒有定義 add_header 指令。

如果指定了 always 參數 (1.7.5),則無論回應碼為何,都會新增標頭欄位。

語法 add_trailer name value [always];
預設
上下文 http, server, location, if in location

此指令出現在 1.13.2 版本中。

將指定的欄位新增到回應的結尾,前提是回應碼等於 200、201、206、301、302、303、307 或 308。參數值可以包含變數。

可以有多個 add_trailer 指令。這些指令會從先前的設定層級繼承,前提是目前層級上沒有定義 add_trailer 指令。

如果指定了 always 參數,則無論回應碼為何,都會新增指定的欄位。

語法 expires [modified] time;
expires epoch | max | off;
預設
expires off;
上下文 http, server, location, if in location

啟用或停用新增或修改 “Expires” 和 “Cache-Control” 回應標頭欄位,前提是回應碼等於 200、201 (1.3.10)、204、206、301、302、303、304、307 (1.1.16, 1.0.13) 或 308 (1.13.0)。參數可以是正或負的時間

“Expires” 欄位中的時間會計算為目前時間與指令中指定的 time 的總和。如果使用 modified 參數 (0.7.0, 0.6.32),則時間會計算為檔案的修改時間與指令中指定的 time 的總和。

此外,可以使用 “@” 前置詞 (0.7.9, 0.6.34) 來指定時間。

expires @15h30m;

“Cache-Control” 欄位的內容取決於指定時間的正負號。

epoch 參數將 “Expires” 設定為值 “Thu, 01 Jan 1970 00:00:01 GMT”,並將 “Cache-Control” 設定為 “no-cache”。

max 參數將 “Expires” 設定為值 “Thu, 31 Dec 2037 23:55:55 GMT”,並將 “Cache-Control” 設定為 10 年。

off 參數會停用新增或修改 “Expires” 和 “Cache-Control” 回應標頭欄位。

最後一個參數值可以包含變數 (1.7.9)

map $sent_http_content_type $expires {
    default         off;
    application/pdf 42d;
    ~image/         max;
}

expires $expires;