多图片网站优化,系统优化大师官方下载,网站建设多少钱裙,wordpress菜单导航栏透明文章目录 1. 背景介绍2. 测试环境搭建3. 缓存字段3.1 Expires3.2 Cache-Control3.3 协商缓存 1. 背景介绍
Http协议标准有RFC定义好的请求和响应头部字段用于进行缓存设置#xff0c;本文主要进行介绍缓存功能相关的头部字段及其使用方法。在使用CDN功能是#xff0c;协议标… 文章目录 1. 背景介绍2. 测试环境搭建3. 缓存字段3.1 Expires3.2 Cache-Control3.3 协商缓存 1. 背景介绍
Http协议标准有RFC定义好的请求和响应头部字段用于进行缓存设置本文主要进行介绍缓存功能相关的头部字段及其使用方法。在使用CDN功能是协议标准缓存字段的识别和判断是十分重要的
缓存字段优先级如下
Cache-ControlPrivate Max-age ExpiresEtag Last-Modified
2. 测试环境搭建
安装Nginx服务且配置conf.d子配置文件目录实验环境ip为内网地址
# 在已经安装好的nginx服务conf主配置文件的http模块下添加如下配置
[rootlocalhost nginx]# cat conf/nginx.conf | grep conf.dinclude /middleware/nginx/conf.d/*.conf; # 该配置代表http模块将conf.d目录下的conf后缀文件也作为配置文件进行加载# 创建备用配置文件目录并且创建新的端口服务
[rootlocalhost nginx]# mkdir conf.d
[rootlocalhost nginx]# cat conf.d/ceshi01.conf
server {listen 8081; # 新端口使用8081server_name localhost;location /ceshi01 {root /middleware/nginx/conf.d/data; # ceshi01访问路径为/middleware/nginx/conf.d/data/ceshi01/ceshi.htmlindex /ceshi01/ceshi.html;default_type text/html;}location /ceshi02 {root /middleware/nginx/conf.d/data; # ceshi02访问路径为/middleware/nginx/conf.d/data/ceshi02/ceshi.htmlindex /ceshi02/ceshi.html;default_type text/html;}error_page 403 404 /middleware/nginx/conf.d/data/40x.html; # 403和404状态码进行特殊配置location /middleware/nginx/conf.d/data/40x.html {root html;}
}# 准备目标访问html文件
[rootlocalhost nginx]# cat /middleware/nginx/conf.d/data/ceshi01/ceshi.html
{uasername: ceshi01, age: 27}
[rootlocalhost nginx]# cat /middleware/nginx/conf.d/data/ceshi02/ceshi.html
{uasername: ceshi02, age: 27}# 重启Nginx服务
[rootlocalhost nginx]# ./sbin/nginx -t # 重启服务前必须进行检测配置文件是否可以正确执行这是一个非常好的习惯
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful # 配置文件可以执行无错误
[rootlocalhost nginx]# ./sbin/nginx -s reload# 功能验证
[rootlocalhost nginx]# curl -Lv http://9.134.244.180:8081/ceshi02/ceshi.html
* Trying 9.134.244.180:8081...
* Connected to 9.134.244.180 (9.134.244.180) port 8081GET /ceshi02/ceshi.html HTTP/1.1Host: 9.134.244.180:8081User-Agent: curl/8.9.1Accept: */** Request completely sent offHTTP/1.1 200 OKServer: nginx/1.25.3Date: Wed, 04 Sep 2024 07:44:30 GMTContent-Type: text/htmlContent-Length: 36Last-Modified: Thu, 29 Aug 2024 10:54:41 GMTConnection: keep-aliveETag: 66d05371-24Accept-Ranges: bytes{uasername: ceshi02, age: 27}
* Connection #0 to host 9.134.244.180 left intact3. 缓存字段
3.1 Expires
Expire字段代表一个固定的缓存过期时间为0代表缓存过期需要注意的是在添加了Expire之后响应会自动加入Cache-Control: max-agexxx的响应头部而这个缓存时间等同于Expire设置的过期时间。
# Nginx配置如下location /ceshi01 {root /middleware/nginx/conf.d/data;index /ceshi01/ceshi.html;default_type text/html;expires 1M; # 设置过期时间为1个月之后}# 结果验证
[rootlocalhost nginx]# curl -Lv http://9.134.244.180:8081/ceshi01/ceshi.html
* Trying 9.134.244.180:8081...
* Connected to 9.134.244.180 (9.134.244.180) port 8081GET /ceshi01/ceshi.html HTTP/1.1Host: 9.134.244.180:8081User-Agent: curl/8.9.1Accept: */** Request completely sent offHTTP/1.1 200 OKServer: nginx/1.25.3Date: Wed, 04 Sep 2024 07:51:09 GMT # 当前请求时间Content-Type: text/htmlContent-Length: 36Last-Modified: Thu, 29 Aug 2024 10:54:32 GMTConnection: keep-aliveETag: 66d05368-24Expires: Fri, 04 Oct 2024 07:51:09 GMT # 设置缓存过期时间的返回字段Cache-Control: max-age2592000 # CC字段标识Accept-Ranges: bytes{uasername: ceshi01, age: 27}
* Connection #0 to host 9.134.244.180 left intact从测试结果可以看出响应头部的 Expires: Fri, 04 Oct 2024 07:51:09 GMT 时间为当前请求时间的一个月之后而 Cache-Control: max-age2592000 代表秒数正好为30days。
3.2 Cache-Control
Cache-Control在http协议标准内为缓存指令头部可以使用如下配置(加粗为常见配置)
是否缓存 public响应可以被任何对象缓存包含不可以被缓存的内容搭配请求方式为POST类型private响应只可以被单个用户缓存不能作为共享缓存即代理服务器不可以缓存no-cache强制要求回源使用协议缓存方式进行验证no-store不使用任何缓存功能。 到期时间 max-agesecounds设置缓存的最大周期xxx为秒级配置时间设置是针对于请求的发起时间而后的过期秒数如发起时间是amax-age1000即过期时间为a1000s之后smax-agesecounds覆盖max-age或Expires头部配置仅适用于共享缓存即代理服务器缓存max-stalesecounds客户端可以允许接受一个过期时间在设置的时间范围内文件min-freshsecounds客户端允许获取一个在指定秒数内保持其最新状态的响应 重新加载与验证 must-revalidate若资源过期在获取到新资源之前不使用旧资源响应后续请求proxy-revalidate与must-revalidate作用一致但只适用于共享缓存模式 其他 no-transform不对资源进行转换或转变Content-Encoding、Content-Range、Content-Type等HTTP头不能由代理修改only-if-cached客户端只接受已经缓存的响应不向源站检测是否有新资源。
# Nginx配置如下location /ceshi01 {root /middleware/nginx/conf.d/data;index /ceshi01/ceshi.html;default_type text/html;# expires 1M;add_header Cache-Control max-age1024; # 设置CC字段max-age1024}# 结果验证
[rootlocalhost nginx]# curl -Lv http://9.134.244.180:8081/ceshi01/ceshi.html
* Trying 9.134.244.180:8081...
* Connected to 9.134.244.180 (9.134.244.180) port 8081GET /ceshi01/ceshi.html HTTP/1.1Host: 9.134.244.180:8081User-Agent: curl/8.9.1Accept: */** Request completely sent offHTTP/1.1 200 OKServer: nginx/1.25.3Date: Wed, 04 Sep 2024 07:56:08 GMTContent-Type: text/htmlContent-Length: 36Last-Modified: Thu, 29 Aug 2024 10:54:32 GMTConnection: keep-aliveETag: 66d05368-24Cache-Control: max-age1024 # 只有CC字段没有ExpiresAccept-Ranges: bytes{uasername: ceshi01, age: 27}
* Connection #0 to host 9.134.244.180 left intact3.3 协商缓存
协商缓存指的是http协议请求和响应的两个字段配合如「Last-Modified If-Modified-Since」 与 「Etag If-None-Match」。
Last-Modified其中包含源头服务器认定的资源做出修改的日期及时间。它通常被用作一个验证器来判断接收到的或者存储的资源是否彼此一致。由于精确度比 ETag 要低所以这是一个备用机制。包含有 If-Modified-Since 或 If-Unmodified-Since 首部的条件请求会使用这个字段EtagHTTP 响应头是资源的特定版本的标识符。这可以让缓存更高效并节省带宽因为如果内容没有改变Web 服务器不需要发送完整的响应。而如果内容发生了变化使用 ETag 有助于防止资源的同时更新相互覆盖“空中碰撞 - 强校验哈希值不匹配情况”。
需要注意的是
Etag的精细程度比Mtime更高Mtime无法解决文件内容不变而创建时间改变的场景Mtime的检测周期是秒级Etag为毫秒级
# Nginx配置如下location /ceshi01 {root /middleware/nginx/conf.d/data;index /ceshi01/ceshi.html;default_type text/html;# expires 1M;add_header Cache-Control no-cache;# add_header Cache-Control no-store;}# 结果验证
[rootlocalhost nginx]# curl -Lv http://9.134.244.180:8081/ceshi01/ceshi.html
* Trying 9.134.244.180:8081...
* Connected to 9.134.244.180 (9.134.244.180) port 8081GET /ceshi01/ceshi.html HTTP/1.1Host: 9.134.244.180:8081User-Agent: curl/8.9.1Accept: */** Request completely sent offHTTP/1.1 200 OKServer: nginx/1.25.3Date: Wed, 04 Sep 2024 08:30:24 GMTContent-Type: text/htmlContent-Length: 36Last-Modified: Thu, 29 Aug 2024 10:54:32 GMTConnection: keep-aliveETag: 66d05368-24Cache-Control: no-cacheAccept-Ranges: bytes{uasername: ceshi01, age: 27}
* Connection #0 to host 9.134.244.180 left intact[rootlocalhost nginx]# stat /middleware/nginx/conf.d/data/ceshi01/ceshi.htmlFile: /middleware/nginx/conf.d/data/ceshi01/ceshi.htmlSize: 36 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 655368 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-08-29 18:54:32.151256443 0800 # 注意时区GMT时间需要8h国内时间
Modify: 2024-08-29 18:54:32.151256443 0800
Change: 2024-08-29 18:59:48.756262327 0800Birth: -当 Cache-Control: no-cache 配置生效之后会强制进行协议缓存校验比对 Last-Modified 字段和源站文件的创建时间如果不一致则返回新资源。
# Nginx配置修改CC字段为不使用任何缓存location /ceshi01 {root /middleware/nginx/conf.d/data;index /ceshi01/ceshi.html;default_type text/html;# expires 1M;# add_header Cache-Control no-cache;add_header Cache-Control no-store;}# 结果验证
[rootlocalhost nginx]# curl -Lv http://9.134.244.180:8081/ceshi01/ceshi.html
* Trying 9.134.244.180:8081...
* Connected to 9.134.244.180 (9.134.244.180) port 8081GET /ceshi01/ceshi.html HTTP/1.1Host: 9.134.244.180:8081User-Agent: curl/8.9.1Accept: */** Request completely sent offHTTP/1.1 200 OKServer: nginx/1.25.3Date: Wed, 04 Sep 2024 08:36:57 GMTContent-Type: text/htmlContent-Length: 36Last-Modified: Thu, 29 Aug 2024 10:54:32 GMTConnection: keep-aliveETag: 66d05368-24Cache-Control: no-storeAccept-Ranges: bytes{uasername: ceshi01, age: 27}
* Connection #0 to host 9.134.244.180 left intact当 Cache-Control: no-store 配置生效后则代表不再使用任何缓存直接从源站获取目标资源。