欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

54)ngx_http_headers_module模块add_header和expires指令

一. 前言

ngx_http_headers_module模块提供了两个重要的指令add_header和expires,来添加 “Expires” 和 “Cache-Control” 头字段,对响应头添加任何域字段。add_header可以用来标示请求访问到哪台服务器上,这个也可以通过nginx模块nginx-http-footer-filter研究使用来实现。expires指令用来对浏览器本地缓存的控制。

二. add_header指令

语法: add_header name value;
默认值: —
配置段: http, server, location, if in location
对响应代码为200,201,204,206,301,302,303,304,或307的响应报文头字段添加任意域。如:

add_header From ttlsa.com

三. 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头中的时间根据目前时间和指令中指定的时间的和来获得。

epoch表示自1970年一月一日00:00:01 GMT的绝对时间,max指定Expires的值为2037年12月31日23:59:59,Cache-Control的值为10 years。
Cache-Control头的内容随预设的时间标识指定:
·设置为负数的时间值:Cache-Control: no-cache。
·设置为正数或0的时间值:Cache-Control: max-age = #,这里#的单位为秒,在指令中指定。
参数off禁止修改应答头中的”Expires”和”Cache-Control”。

实例一:对图片,flash文件在浏览器本地缓存30天

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 {
           expires 30d;
 }

实例二:对js,css文件在浏览器本地缓存1小时

location ~ .*\.(js|css)$
 {
            expires 1h;
 }

如需转载请注明出处:http://www.ttlsa.com/html/3068.html

 

版权说明

文章标题: ngx_http_headers_module模块add_header和expires指令
本文地址: http://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/
除非注明,博客文章均为"运维生存时间"原创,转载请标明本文地址
交流群:①群39514058(满)、②群6690706(满)、③群168085569(新)

来自 http://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/
普通分类: