Module ngx_http_headers_module
ngx_http_headers_module
模块允许将“ Expires”和“ Cache-Control”头字段以及任意字段添加到响应头中。
Example Configuration
expires 24h;
expires modified +24h;
expires @24h;
expires 0;
expires -1;
expires epoch;
expires $expires;
add_header Cache-Control private;
Directives
Syntax: | add_header name value [always]; |
Default: | — |
Context: | 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),则将指定字段添加到响应 Headers.0)。参数值可以包含变量。
可能有多个add_header
指令。当且仅当当前级别上没有定义add_header
指令时,这些指令才从上一级继承。
如果指定了always
参数(1.7.5),则无论响应代码如何,都将添加 Headers 字段。
Syntax: | add_trailer name value [always]; |
Default: | — |
Context: | http , server , location , if in location |
该指令出现在 1.13.2 版中。
如果响应代码等于 200、201、206、301、302、303、307 或 308,则将指定的字段添加到响应的末尾。参数值可以包含变量。
可能有多个add_trailer
指令。当且仅当当前级别上没有定义add_trailer
指令时,这些指令才从上一级继承。
如果指定了always
参数,则无论响应代码如何,都会添加指定的字段。
Syntax: | expires [modified] time; |
expires epoch | max | off; | |
默认值: | expires off; |
上下文: http ,server ,location ,if in location |
如果响应代码等于 200、201(1.3.10),204、206、301、302、303、304、307(1.1. ),则启用或禁用“ Expires”和“ Cache-Control”响应 Headers 字段的添加或修改。 16、1.0.13)或 308(1.13.0)。参数可以是正数或负数time。
“ Expires”字段中的时间计算为当前时间与指令中指定的time
之和。如果使用了modified
参数(0.7.0、0.6.32),则该时间将作为文件的修改时间与指令中指定的time
之和来计算。
此外,还可以使用“ @
”前缀(0.7.9,0.6.34)指定一天中的时间:
expires @15h30m;
“ Cache-Control”字段的内容取决于指定时间的符号:
-
时间为负数-“缓存控制:无缓存”。
-
time 为正数或为零—“ Cache-Control:max-age =
t
”,其中t
是指令中指定的时间,以秒为单位。
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;