IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx设置Cache-Control

    五月里徜徉的小猫咪发表于 2014-01-09 16:42:34
    love 0

    今天弄了一天,本来是直接设置主题,结果用的meta标签看起来没啥用,于是就改用了nginx,使用的是这个ngx_http_headers_module,非常好用,可以直接设置Cache-Control的请求头。

    用法示例

    expires 24h;
    expires modified +24h;
    expires @24h;
    expires 0;
    expires -1;
    expires epoch;
    add_header Cache-Control private;

    需要说明的是,expires只能用一个,@是表示具体的时间。

    语法:add_header name value;
    默认:	 —
    适用区间:	http, server, location, if in location

    适用于 200, 201, 204, 206, 301, 302, 303, 304, 或者307请求

    语法:expires [modified] time;
    expires epoch | max | off;
    默认值:	expires off;
    适用区间:	http, server, location, if in location

    同样适用于 200, 201, 204, 206, 301, 302, 303, 304, 或者 307这些HTTP请求。

    epoch表示 “Thu, 01 Jan 1970 00:00:01 GMT”,max则表示 “Thu, 31 Dec 2037 23:55:55 GMT”同时Cach-control会被设置为十年。

    安装

    如果不支持,编译时候加入–with-ngx_http_headers_module即可了,Tengine则默认支持。

    效果

    HTTP/1.1 200 OK
    Server: Tengine/1.5.2
    Date: Thu, 09 Jan 2014 16:09:41 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Vary: Accept-Encoding
    X-Powered-By: PHP/5.5.6
    Set-Cookie: PHPSESSID=vqfpqsgr8d9du2h0tneok4o2f0; path=/
    Expires: Fri, 10 Jan 2014 16:09:41 GMT
    Cache-Control: max-age=86400
    Pragma: no-cache
    X-Pingback: http://catinmay.com/xmlrpc.php
    Link: <http://wp.me/4cIyj>; rel=shortlink
    Cache-Control: public

    更新

    使用add_header Last-Modified $sent_http_Expires;语句添加Last-Modified时间戳

    HTTP/1.1 200 OKServer: nginx/1.4.1
    Date: Fri, 10 Jan 2014 01:02:24 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Vary: Accept-Encoding
    X-Powered-By: PHP/5.5.6
    Set-Cookie: PHPSESSID=q67op8s83vo88stc4shbd0thq2; path=/
    Expires: Sat, 11 Jan 2014 01:02:24 GMT
    Cache-Control: max-age=86400
    Pragma: no-cache
    X-Pingback: http://catinmay.com/xmlrpc.php
    Link: <http://wp.me/4cIyj>; rel=shortlink
    Cache-Control: public
    Last-Modified: Sat, 11 Jan 2014 01:02:24 GMT



沪ICP备19023445号-2号
友情链接