nginx中文文档-ngx_http_headers_module

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

语法:add_header name value [always]
默认:—
上下文:http, server, location, if in location

为响应码等于200, 201, 204, 206, 301, 302, 303, 304, 或 307的响应增加指定的头域。值可以包含变量。
可以由多个add_header指令。如果当前层级没有add_header指令,则这些指令会被从上级继承。
如果指定了always参数(1.7.5+),会忽略响应码直接添加响应头域。

expires

语法:expires [modified] time
expires epoch | max | off
默认:expires off
上下文:http, server, location, if in location

为响应码等于200, 201, 204, 206, 301, 302, 303, 304, 或 307的响应启用或禁用添加或修改“Expires” 和 “Cache-Control” 字段的功能。参数可以是正数或负数。
“Expires”域中的时间由当前时间和指令中指定的时间之和计算得出。如果modified参数使用了(0.7.0+,0.6.32+),那么计算文件修改时间和指令中时间之和。
另外,可以通过“@”前缀指定一天中的时间(0.7.9+,0.6.34+):
expires @15h30m;
epoch参数相当于绝对时间“Thu, 01 Jan 1970 00:00:01 GMT”。
“Cache-Control”字段的值取决于指定的之间:

  • 时间是负数-“Cache-Control: no-cache”
  • 时间是正数或0-“Cache-Control: max-age=t”,其中t是指令中定义的时间,单位为秒。

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;

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

请开启浏览器JavaScript