模块 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 名称 [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 名称 [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] 时间;
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.16, 1.0.13), 或 308 (1.13.0) 时,启用或禁用添加或修改“Expires”和“Cache-Control”响应头部字段。参数可以是正数或负数时间

“Expires”字段中的时间计算为当前时间与指令中指定的 时间 之和。如果使用了 modified 参数 (0.7.0, 0.6.32),则时间计算为文件的修改时间与指令中指定的 时间 之和。

此外,可以使用“@”前缀指定一天中的时间 (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;