浏览器缓存 - 配置示例

1. 概要

  • 浏览器的缓存基于HTTP的缓存机制(如:Expires; Cache-control等),其实就是一些头信息;

2. 配置语法

Syntax: expires [modified] time;
        expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in action

3. 不加expires - 实验过程

3.1 第一次请求资源 -> 返回200

3.2 第二次请求资源 -> 返回304

  • Request Headers
    • Cache-Control:max-age=0(浏览器自己加的,基于浏览器自己的实现逻辑)

4. 加expires - 实验过程

4.1 配置语法

[root@localhost ~]# vim /etc/nginx/conf.d/static_server.conf
server {
    listen       80;

    location / {
        expires 24h;
        root  /opt/app/code;
    }

    error_page   500 502 503 504 404  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

4.2 重载nginx服务

[root@localhost ~]# nginx -s reload -c /etc/nginx/nginx.conf

4.3 基于之前实验结果,刷新网页 -> 返回304

  • Request Headers
    • Cache-Control:max-age=0(浏览器自己加的,基于浏览器自己的实现逻辑)
  • Reponse Headers
    • Cache-Control:max-age=86400
    • Expires:Mon, 27 Nov 2017 09:38:02 GMT

注意:
Reponse Headers中已经有Cache-Control:max-age=86400和Expires:Mon, 27 Nov 2017 09:38:02 GMT了,但是浏览器不会遵循服务端返回的设置。

你可能感兴趣的:(浏览器缓存 - 配置示例)