wordpress全站靜態(tài)頁面百度網(wǎng)站排名怎么提高
文章目錄
- 1. 背景介紹
- 2. 測試環(huán)境搭建
- 3. 緩存字段
- 3.1 Expires
- 3.2 Cache-Control
- 3.3 協(xié)商緩存
1. 背景介紹
Http協(xié)議標(biāo)準(zhǔn)有RFC定義好的請求和響應(yīng)頭部字段用于進(jìn)行緩存設(shè)置,本文主要進(jìn)行介紹緩存功能相關(guān)的頭部字段及其使用方法。在使用CDN功能是,協(xié)議標(biāo)準(zhǔn)緩存字段的識別和判斷是十分重要的!
緩存字段優(yōu)先級如下:
Cache-Control(Private > Max-age) > Expires
Etag > Last-Modified
2. 測試環(huán)境搭建
安裝Nginx服務(wù),且配置conf.d子配置文件目錄(實(shí)驗(yàn)環(huán)境ip為內(nèi)網(wǎng)地址)
# 在已經(jīng)安裝好的nginx服務(wù)conf主配置文件的http模塊下添加如下配置
[root@localhost nginx]# cat conf/nginx.conf | grep 'conf.d'include /middleware/nginx/conf.d/*.conf; # 該配置代表http模塊將conf.d目錄下的conf后綴文件也作為配置文件進(jìn)行加載# 創(chuàng)建備用配置文件目錄并且創(chuàng)建新的端口服務(wù)
[root@localhost nginx]# mkdir conf.d
[root@localhost 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狀態(tài)碼進(jìn)行特殊配置location = /middleware/nginx/conf.d/data/40x.html {root html;}
}# 準(zhǔn)備目標(biāo)訪問html文件
[root@localhost nginx]# cat /middleware/nginx/conf.d/data/ceshi01/ceshi.html
{'uasername': 'ceshi01', 'age': 27}
[root@localhost nginx]# cat /middleware/nginx/conf.d/data/ceshi02/ceshi.html
{'uasername': 'ceshi02', 'age': 27}# 重啟Nginx服務(wù)
[root@localhost nginx]# ./sbin/nginx -t # 重啟服務(wù)前必須進(jìn)行檢測配置文件是否可以正確執(zhí)行,這是一個非常好的習(xí)慣!
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 # 配置文件可以執(zhí)行無錯誤
[root@localhost nginx]# ./sbin/nginx -s reload# 功能驗(yàn)證
[root@localhost 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 8081
> GET /ceshi02/ceshi.html HTTP/1.1
> Host: 9.134.244.180:8081
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.25.3
< Date: Wed, 04 Sep 2024 07:44:30 GMT
< Content-Type: text/html
< Content-Length: 36
< Last-Modified: Thu, 29 Aug 2024 10:54:41 GMT
< Connection: keep-alive
< ETag: "66d05371-24"
< Accept-Ranges: bytes
<
{'uasername': 'ceshi02', 'age': 27}
* Connection #0 to host 9.134.244.180 left intact
3. 緩存字段
3.1 Expires
Expire字段代表一個固定的緩存過期時間,為0代表緩存過期,需要注意的是,在添加了Expire之后,響應(yīng)會自動加入Cache-Control: max-age=xxx的響應(yīng)頭部,而這個緩存時間等同于Expire設(shè)置的過期時間。
# Nginx配置如下location /ceshi01 {root /middleware/nginx/conf.d/data;index /ceshi01/ceshi.html;default_type text/html;expires 1M; # 設(shè)置過期時間為1個月之后}# 結(jié)果驗(yàn)證
[root@localhost 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 8081
> GET /ceshi01/ceshi.html HTTP/1.1
> Host: 9.134.244.180:8081
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.25.3
< Date: Wed, 04 Sep 2024 07:51:09 GMT # 當(dāng)前請求時間
< Content-Type: text/html
< Content-Length: 36
< Last-Modified: Thu, 29 Aug 2024 10:54:32 GMT
< Connection: keep-alive
< ETag: "66d05368-24"
< Expires: Fri, 04 Oct 2024 07:51:09 GMT # 設(shè)置緩存過期時間的返回字段
< Cache-Control: max-age=2592000 # CC字段標(biāo)識
< Accept-Ranges: bytes
<
{'uasername': 'ceshi01', 'age': 27}
* Connection #0 to host 9.134.244.180 left intact
從測試結(jié)果可以看出響應(yīng)頭部的 Expires: Fri, 04 Oct 2024 07:51:09 GMT 時間為當(dāng)前請求時間的一個月之后,而 Cache-Control: max-age=2592000 代表秒數(shù),正好為30days。
3.2 Cache-Control
Cache-Control在http協(xié)議標(biāo)準(zhǔn)內(nèi)為緩存指令頭部,可以使用如下配置(加粗為常見配置):
- 是否緩存
public
:響應(yīng)可以被任何對象緩存,包含不可以被緩存的內(nèi)容;搭配請求方式為POST類型;private
:響應(yīng)只可以被單個用戶緩存,不能作為共享緩存(即代理服務(wù)器不可以緩存);no-cache
:強(qiáng)制要求回源使用協(xié)議緩存方式進(jìn)行驗(yàn)證;no-store
:不使用任何緩存功能。
- 到期時間
max-age=<secounds>
:設(shè)置緩存的最大周期,xxx為秒級配置,時間設(shè)置是針對于請求的發(fā)起時間而后的過期秒數(shù),如發(fā)起時間是a,max-age=1000,即過期時間為a+1000s之后;smax-age=<secounds>
:覆蓋max-age或Expires頭部配置,僅適用于共享緩存(即代理服務(wù)器緩存);max-stale=<secounds>
:客戶端可以允許接受一個過期時間在設(shè)置的時間范圍內(nèi)文件;min-fresh=<secounds>
:客戶端允許獲取一個在指定秒數(shù)內(nèi)保持其最新狀態(tài)的響應(yīng);
- 重新加載與驗(yàn)證
must-revalidate
:若資源過期,在獲取到新資源之前不使用舊資源響應(yīng)后續(xù)請求;proxy-revalidate
:與must-revalidate作用一致,但只適用于共享緩存模式;
- 其他
no-transform
:不對資源進(jìn)行轉(zhuǎn)換或轉(zhuǎn)變,Content-Encoding、Content-Range、Content-Type等HTTP頭不能由代理修改;only-if-cached
:客戶端只接受已經(jīng)緩存的響應(yīng),不向源站檢測是否有新資源。
# Nginx配置如下location /ceshi01 {root /middleware/nginx/conf.d/data;index /ceshi01/ceshi.html;default_type text/html;# expires 1M;add_header Cache-Control max-age=1024; # 設(shè)置CC字段max-age=1024}# 結(jié)果驗(yàn)證
[root@localhost 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 8081
> GET /ceshi01/ceshi.html HTTP/1.1
> Host: 9.134.244.180:8081
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.25.3
< Date: Wed, 04 Sep 2024 07:56:08 GMT
< Content-Type: text/html
< Content-Length: 36
< Last-Modified: Thu, 29 Aug 2024 10:54:32 GMT
< Connection: keep-alive
< ETag: "66d05368-24"
< Cache-Control: max-age=1024 # 只有CC字段,沒有Expires
< Accept-Ranges: bytes
<
{'uasername': 'ceshi01', 'age': 27}
* Connection #0 to host 9.134.244.180 left intact
3.3 協(xié)商緩存
協(xié)商緩存指的是http協(xié)議請求和響應(yīng)的兩個字段配合,如:「Last-Modified + If-Modified-Since」 與 「Etag + If-None-Match」。
Last-Modified
:其中包含源頭服務(wù)器認(rèn)定的資源做出修改的日期及時間。它通常被用作一個驗(yàn)證器來判斷接收到的或者存儲的資源是否彼此一致。由于精確度比 ETag 要低,所以這是一個備用機(jī)制。包含有 If-Modified-Since 或 If-Unmodified-Since 首部的條件請求會使用這個字段Etag
:HTTP 響應(yīng)頭是資源的特定版本的標(biāo)識符。這可以讓緩存更高效,并節(jié)省帶寬,因?yàn)槿绻麅?nèi)容沒有改變,Web 服務(wù)器不需要發(fā)送完整的響應(yīng)。而如果內(nèi)容發(fā)生了變化,使用 ETag 有助于防止資源的同時更新相互覆蓋(“空中碰撞 - 強(qiáng)校驗(yàn)哈希值不匹配情況”)。
需要注意的是:
- Etag的精細(xì)程度比Mtime更高,Mtime無法解決文件內(nèi)容不變而創(chuàng)建時間改變的場景;
- 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;}# 結(jié)果驗(yàn)證
[root@localhost 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 8081
> GET /ceshi01/ceshi.html HTTP/1.1
> Host: 9.134.244.180:8081
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.25.3
< Date: Wed, 04 Sep 2024 08:30:24 GMT
< Content-Type: text/html
< Content-Length: 36
< Last-Modified: Thu, 29 Aug 2024 10:54:32 GMT
< Connection: keep-alive
< ETag: "66d05368-24"
< Cache-Control: no-cache
< Accept-Ranges: bytes
<
{'uasername': 'ceshi01', 'age': 27}
* Connection #0 to host 9.134.244.180 left intact[root@localhost 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 # 注意時區(qū),GMT時間需要+8h=國內(nèi)時間
Modify: 2024-08-29 18:54:32.151256443 +0800
Change: 2024-08-29 18:59:48.756262327 +0800Birth: -
當(dāng) Cache-Control: no-cache 配置生效之后,會強(qiáng)制進(jìn)行協(xié)議緩存校驗(yàn),比對 Last-Modified 字段和源站文件的創(chuàng)建時間,如果不一致則返回新資源。
# 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;}# 結(jié)果驗(yàn)證
[root@localhost 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 8081
> GET /ceshi01/ceshi.html HTTP/1.1
> Host: 9.134.244.180:8081
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.25.3
< Date: Wed, 04 Sep 2024 08:36:57 GMT
< Content-Type: text/html
< Content-Length: 36
< Last-Modified: Thu, 29 Aug 2024 10:54:32 GMT
< Connection: keep-alive
< ETag: "66d05368-24"
< Cache-Control: no-store
< Accept-Ranges: bytes
<
{'uasername': 'ceshi01', 'age': 27}
* Connection #0 to host 9.134.244.180 left intact
當(dāng) Cache-Control: no-store 配置生效后,則代表不再使用任何緩存,直接從源站獲取目標(biāo)資源。