模块:ngx_http_headers_module
expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求要配置expires,可以在http段中或者server段中或者location段中加入。 Nginx(expires 缓存减轻服务端压力)
语法:
Syntax: expires [modified] time;
expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in location
- epoch:指定“Expires”的值为 1 January,1970,00:00:01 GMT
- max:指定“Expires”的值为10年。
- -1:指定“Expires”的值为当前服务器时间-1s,即永远过期。
- off:不修改“Expires”和"Cache-Control"的值
原理:
无缓存,每次访问服务器,均是全文传输。 开启缓存可以加速浏览网站。
开启浏览器缓存,浏览页面
第一次返回状态码200.页面对象全文传输
第二次返回状态304.页面对象部分传输。
用缓存。浏览页面
返回码200.全文传输
解析缓存原理
开启缓存模块
vim /etc/nginx/conf.d/default.conf
location / {
root /usr/share/nginx/html
index index.html index.htm;
expires 24h;
}
systemctl restart nginx
再次浏览页面,出现服务器回复的缓存时间
- 启动缓存时间,加速浏览
- 缺点是时效性降低