计算机网络/计算机科学与应用/系统/运维/开发

Nginx 浏览器本地缓存设置expires

浏览器缓存为了加速浏览,本地的资源,节省网络资源


语法:

expires [time |epoch | max | off]


time:使用正数或负数

负数:Cache-Control: no-cache

正数或零:Cache-Control:max-age=#,# 为您指定时间的秒数

epoch:指定"Expires"的值为1 January, 1970, 00:00:01 GMT

max:指定"Expires"的值为31 December 2037 23:59:59 GMT,“Cache-Control”的值为10年

-1指定“Expires”的值为服务器当前时间-1s,即永远过期

off表示不修改“Expires”和“Cache-Control”的值


默认值: 

expires off

作用域:

http/server/location

作用:

该指令控制http应答中的 "Expires" 和 "Cache-Control" header头信息(控制页面缓存作用)


实例:对图片 css文件进行缓存

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

expires 格式:

expires 30s; 缓存30秒

expires 30m;  30分钟

expires 2h; 2小时

expires 30d; 30天


注意:

这种缓存方式只能在用户不对浏览器强制刷新的情况下生效,如果用户通过url来进行访问,是可以访问到缓存的


顺,不妄喜;逆,不惶馁;安,不奢逸;危,不惊惧。--史记

评论

^