Nginx ngx_http_headers_module模块基本指令整理

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的ngx_http_headers_module 模块做简单介绍,本文具体包括如下指令:add_header, expires

该ngx_http_headers_module模块允许发出“Expires”和“Cache-Control”控制头域,并可以添加其他头部与到相应头中。

配置示例:

expires    24h;

expires    modified +24h;

expires    @24h;

expires    0;

expires    -1;

expires    epoch;

add_header Cache-Control private;

;

Nginx原文:

The ngx_http_headers_module module allows to emit the “Expires” and “Cache-Control” header fields, and to add arbitrary fields to a response header.

Example Configuration

expires    24h;

expires    modified +24h;

expires    @24h;

expires    0;

expires    -1;

expires    epoch;

add_header Cache-Control private;

;

1. add_header

syntax:add_header  name  value;
default:
context:http, server, location, if in location

向相应代码为200, 201, 204, 206, 301, 302, 303, 304, or 307的相应(response)中添加指定的相应头。头的值可以为一个变量。

Nginx原文:

Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables.

2. expires

syntax:expires   [modified] time;
expires   epoch | max | off;
default:expires off;
context:http, server, location, if in location

向相应代码为200, 201, 204, 206, 301, 302, 303, 304, or 307的相应中开启或者关闭添加或者改变“Expires” 和“Cache-Control”头部域的功能(若是开启,这添加或者改变相应头部域)。 参数值(时间)可以为正也可以为负

“Expires” 的时间作为当前指令指定的时间与当前时间(系统时间)的和。如果modified 参数被指定, 时间计算做文件修改时间与指令的指定时间的和

此外,有可能使用“@”前缀指定一个时间每天,例如:expires @15h30m;

epoch 参数对于绝对时间“Thu, 01 Jan 1970 00:00:01 GMT”, (当指定这个参数后,相应域中 “Cache-Control”值依赖于指令中指定的特定时间)

时间参数是负数时, “Cache-Control: no-cache”.

时间常数是整数或者0时: “Cache-Control: max-age=t”, t是指令中指定的时间参数,单位是秒

“Expires”的最大值是 “Thu, 31 Dec 2037 23:55:55 GMT”, “Cache-Control”最大值是10年

Off参数禁止向相应头中添加或者改变相应头中“Expires” 和“Cache-Control”参数。

Nginx原文:

Enables or disables adding or modifying the “Expires” and “Cache-Control” response header fields provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307. A parameter can be a positive or negative time.

A time in the “Expires” field is computed as a sum of the current time and time specified in the directive. If the modified parameter is used (0.7.0, 0.6.32) then time is computed as a sum of the file’s modification time and time specified in the directive.

In addition, it is possible to specify a time of the day using the “@” prefix (0.7.9, 0.6.34):

expires @15h30m;

The epoch parameter corresponds to the absolute time “Thu, 01 Jan 1970 00:00:01 GMT”. The contents of the “Cache-Control” field depends on the sign of the specified time:

time is negative — “Cache-Control: no-cache”.

time is positive or zero — “Cache-Control: max-age=t”, where t is a time specified in the directive, in seconds.

The max parameter sets “Expires” to the value “Thu, 31 Dec 2037 23:55:55 GMT”, and “Cache-Control” to 10 years.

The off parameter disables adding or modifying the “Expires” and “Cache-Control” response header fields.

发表评论