web優(yōu)化 之 緩存

目錄

準(zhǔn)備

tree ~/docker/nginx-cache
/Users/kevin/docker/nginx-cache
├── conf
│   └── nginx.conf
└── html
    ├── index.html
    └── index.js

2 directories, 3 files
  • nginx.conf
cat ~/docker/nginx-cache/conf/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    # server {
    #     listen 80;
    #     server_name console.zhgcloud.com;
    #     location / {
    #         root /usr/share/nginx/html;
    #         try_files /index.html $uri;
    #     }
    # }

    # server {
    #     listen 80;
    #     server_name a.zhgcloud.com;
    #     location ~* \.js$ {
    #         root /usr/share/nginx/html;
    #         add_header Cache-Control "max-age=20";
    #         add_header Cache-Control "max-age=31536000";
    #     }
    # }
}

以上nginx.conf與nginx1.10.3默認(rèn)配置相同

  • index.html
cat ~/docker/nginx-cache/html/index.html
<!DOCTYPE html>
<html>

<head>
    <title>Welcome to nginx!</title>
    <script src="http://a.zhgcloud.com/index.js"></script>
</head>

<body>
    <h1>Welcome to nginx!</h1>
</body>

</html>
  • index.js
cat ~/docker/nginx-cache/html/index.js
console.log('from index.js');

上述文件和代碼 在這里可以找到

服務(wù)

  • nginx
docker pull nginx:1.10.3 // 最近的Ubuntu LTS 1604官方源的nginx版本就是1.10.3

docker run -d --name nginx-cache -p 80:80 -v ~/docker/nginx-cache/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/docker/nginx-cache/html:/usr/share/nginx/html/ nginx:1.10.3

curl localhost

docker詳細(xì)介紹 參考Docker入門

  • nginx.conf
vim ~/docker/nginx-cache/conf/nginx.conf
{
    # 省略未改動(dòng)部分
    server {
        listen 80;
        server_name console.zhgcloud.com;
        location / {
            root /usr/share/nginx/html;
            try_files /index.html $uri;
        }
    }

    server {
        listen 80;
        server_name a.zhgcloud.com;
        location ~* \.js$ {
            root /usr/share/nginx/html;
            # add_header Cache-Control "max-age=20";
            # add_header Cache-Control "max-age=31536000";
        }
    }
}
docker restart nginx-cache
  • hosts
sudo vim /etc/hosts
127.0.0.1   console.zhgcloud.com
127.0.0.1   a.zhgcloud.com

瀏覽器緩存

cache-01.png
cache-02.png

關(guān)于瀏覽器緩存時(shí)長(zhǎng) 可以參考How long does Google Chrome cache a resource if expires and/or no-cache headers are not set? / Why is this response being cached?

協(xié)商緩存

cache-03.png
cache-04.png

協(xié)商緩存基于以下兩個(gè)方面

Response Header: ETag

Request Header: If-None-Match

關(guān)于304 Not Modified更多介紹 可以參考304 Not Modified

  • 更新Etag
touch ~/docker/nginx-cache/html/index.js
cache-05.png

強(qiáng)制緩存

  • nginx.conf
vim ~/docker/nginx-cache/conf/nginx.conf
{
    # 省略未改動(dòng)部分
    server {
        listen 80;
        server_name console.zhgcloud.com;
        location / {
            root /usr/share/nginx/html;
            try_files /index.html $uri;
        }
    }

    server {
        listen 80;
        server_name a.zhgcloud.com;
        location ~* \.js$ {
            root /usr/share/nginx/html;
            add_header Cache-Control "max-age=20";
            # add_header Cache-Control "max-age=31536000";
        }
    }
}
docker restart nginx-cache
cache-06.png
cache-07.png
cache-08.png

自己動(dòng)手: 1: 修改Cache-Control驗(yàn)證瀏覽器緩存周期是否會(huì)隨之更新? 2: Cache-Control緩存周期內(nèi)更新Etag是否會(huì)影響瀏覽器請(qǐng)求?

小結(jié)

原理 HTTP請(qǐng)求
瀏覽器緩存 本地緩存 不發(fā)送 & from local cache
協(xié)商緩存 ETag/If-None-Match & Last-Modified/If-Modified-Since 發(fā)送 & 返回304
強(qiáng)制緩存 Cache-Control & Expires 不發(fā)送 & from local cache

綜上所述 完整的前端緩存方案如下

參考

更多文章, 請(qǐng)支持我的個(gè)人博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容