瀏覽器緩存遵循HTTP協議定義的緩存機制(如:Expires;Cache-control等)。
當瀏覽器無緩存時,請求響應流程Syntax: expires [modified] time; expires epoch | max | off;Default: expires off;Context: http, server, location, if in location
本配置項可以控制HTTP響應中的“Expires”和“Cache-Control”頭信息,(起到控制頁面緩存的作用)。
“Expires”頭信息中的過期時間為當前系統時間與您設定的 time 值時間的和。如果指定了 modified 參數,則過期時間為文件的最后修改時間與您設定的 time 值時間的和。
“Cache-Control”頭信息的內容取決于指定 time 的符號??梢栽趖ime值中使用正數或負數。
當 time 為負數,“Cache-Control: no-cache”;
當 time 為正數或0,“Cache-Control: max-age=time”,單位是秒。
epoch 參數用于指定“Expires”的值為 1 January, 1970, 00:00:01 GMT。
max 參數用于指定“Expires”的值為 “Thu, 31 Dec 2037 23:55:55 GMT”,“Cache-Control” 的值為10 年。
off 參數令對“Expires” 和 “Cache-Control”響應頭信息的添加或修改失效。
server { location ~ .*/.(txt|xml)$ { # 設置過期時間為1天 expires 1d; root /vagrant/doc;}2. nginx -s reload 重新載入nginx配置文件3. 創建 /vagrant/doc/hello.txt 文件4. 通過curl訪問 192.168.33.88/hello.txt,查看http響應頭信息
[root/etc/nginx]# curl -I 192.168.33.88/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Tue, 17 Jul 2018 07:12:11 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Expires: Wed, 18 Jul 2018 07:12:11 GMTCache-Control: max-age=86400Accept-Ranges: bytes
重點查看 Expires 和 Cache-Control兩個字段,可見,hello.txt 的緩存時間為1天。
二、防盜鏈目的:防止資源被盜用
思路:區別哪些請求是非正常的用戶請求
Syntax: valid_referers none | blocked | server_names | string ...;Default: —Context: server, location
none:請求頭中沒有 Referer 字段
blocked:請求頭中雖然存在“Referer”字段,但是它的值已經被防火墻或代理服務器刪除;這些值是不以“http://”或“https://”開頭的字符串;
server_names:“Referer”請求頭字段包含該服務器名稱
任意字符串:定義一個服務器名稱和一個可選的URI前綴。服務器名開始或結尾可以有 “*” 。檢查時,“Referer”字段中的服務器端口會被忽略。
正則表達式:字符串必須以 ~ 開頭,值得注意的是,正則表達式匹配的是在“http://”或“https://”之后的內容。
valid_referers none blocked server_names *.example.com example.* www.example.org/galleries/ ~/.google/.;2. 應用實例1. vim conf.d/static.conf
server { location ~ .*/.(txt|xml)$ { # 配置防盜鏈規則 valid_referers none blocked 192.168.1.110 *.example.com example.* ~/.google/.; # 如果不符合防盜鏈規則,則返回403 if ($invalid_referer) { return 403; root /vagrant/doc;}2. nginx -s reload 重新載入nginx配置文件3. 創建 /vagrant/doc/hello.txt 文件
vim /vagrant/a/a.txt
Hello world!4. 使用 curl進行訪問測試
不帶referer,可以正常訪問
[root~]# curl -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Fri, 03 Aug 2018 01:34:12 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Accept-Ranges: bytes
referer為 http://www.baidu.com,返回403
[root~]# curl -e http://www.baidu.com -I http://127.0.0.1/hello.txtHTTP/1.1 403 ForbiddenServer: nginx/1.14.0Date: Fri, 03 Aug 2018 01:34:34 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive
referer為 http://192.168.1.110,可以正常訪問
[root~]# curl -e http://192.168.1.110 -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 2018 11:31:51 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Accept-Ranges: bytes
referer以 example.開頭或 .example.com 結尾,可以正常訪問
[root~]# curl -e http://www.example.com -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 2018 11:33:47 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Accept-Ranges: bytes[root~]# curl -e http://example.baidu.com -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 2018 11:33:53 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Accept-Ranges: bytes
referer為 http://192.168.1.110,可以正常訪問
[root~]# curl -e http://192.168.1.110 -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 2018 11:31:51 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Accept-Ranges: bytes
referer為 http://google.com,返回403
[root~]# curl -e http://google.com -I http://127.0.0.1/hello.txtHTTP/1.1 403 ForbiddenServer: nginx/1.14.0Date: Thu, 02 Aug 2018 11:37:43 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive
referer為 http://www.google.com,可以正常訪問
[root~]# curl -e http://www.google.com -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 2018 11:37:50 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 2018 07:07:22 GMTConnection: keep-aliveETag: 5b4d95aa-c Accept-Ranges: bytes
相關文章推薦:
Nginx作為靜態資源web服務并進行靜態資源壓縮
以上就是Nginx作為靜態資源web服務來控制瀏覽器緩存以及實現防盜鏈的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選