ngx_http_headers_module模块add_header和expires指令

默北 Nginx1 27,9351字数 1030阅读3分26秒阅读模式

一. 前言

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的响应报文头字段添加任意域。如:文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/

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头中的时间根据目前时间和指令中指定的时间的和来获得。文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/

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"。文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/

实例一:对图片,flash文件在浏览器本地缓存30天文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/

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

实例二:对js,css文件在浏览器本地缓存1小时文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/

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

如需转载请注明出处:https://www.ttlsa.com/html/3068.html文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/ 文章源自运维生存时间-https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 18/09/2013 16:53:55
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/the-ngx_http_headers_module-module-add_header-and-expires-instructions/
  • add_header
  • expires
  • nginx
  • ngx_http_headers_module
评论  1  访客  0