Nginx作为静态web服务器——缓存原理

浏览器缓存

客户端无缓存的情况下

客户端有缓存的情况下

校验过期机制

本地客户端会检查Cache-Control(max-age)缓存是否过期,(max-age)为过期时间

Last-Modified

上次修改时间
 配合If-Modified-Since或者If-Unmodified-Since使用
 对比上次修改时间验证资源是否需要更新

ETag

数据签名
典型做法:对资源内容进行hash计算
配合If-Match或者Id-Non-Match使用
对比资源的签名判断是否使用缓存

请求原理

配置语法-expires

添加Cache-Control、Expires头

Syntax:expires [modified] time;

              expires epoch |max|off;

Default:expires off;

Context:http,server,location,if in location

Cache-Control:max-age=0表示每次请求都跟服务器校验Last-Modified

配置语法-expires演示

将配置语法加进去

location ~ .*.(htm|html) {
        expires 24h;            # 24小时查看一次是否更新
        root   /opt/app/code;
    }

检查语法后,重启nginx,访问地址http://192.168.96.188/test_expires.html

这时候头部信息Cache-Control:max-age=86400表示86400秒跟服务器校验Last-Modified

原文地址:https://www.cnblogs.com/joy-sir/p/12162515.html