1. 概要
- 瀏覽器的緩存基于HTTP的緩存機(jī)制(如: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(瀏覽器自己加的钥弯,基于瀏覽器自己的實現(xiàn)邏輯)
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服務(wù)
[root@localhost ~]# nginx -s reload -c /etc/nginx/nginx.conf
4.3 基于之前實驗結(jié)果,刷新網(wǎng)頁 -> 返回304
- Request Headers
- Cache-Control:max-age=0(瀏覽器自己加的,基于瀏覽器自己的實現(xiàn)邏輯)
- Reponse Headers
- Cache-Control:max-age=86400
- Expires:Mon, 27 Nov 2017 09:38:02 GMT
注意:
Reponse Headers中已經(jīng)有Cache-Control:max-age=86400和Expires:Mon, 27 Nov 2017 09:38:02 GMT了针饥,但是瀏覽器不會遵循服務(wù)端返回的設(shè)置。