Nginx實踐篇(2)- Nginx做爲靜態資源web服務 - 控制瀏覽器緩存、防盜鏈

1、控制瀏覽器緩存

1. 瀏覽器緩存簡介

瀏覽器緩存遵循HTTP協議定義的緩存機制(如:Expires;Cache-control等)。html

當瀏覽器無緩存時,請求響應流程

當瀏覽器無緩存時,請求響應流程

當瀏覽器有緩存時,請求響應流程

當瀏覽器有緩存時,請求響應流程

瀏覽器緩存校驗過時機制

校驗是否過時 Cache-Control(max-age)、Expires
協議中Etag頭信息校驗 Etag
Last-Modified頭信息校驗 Last-Modified

瀏覽器請求流程

瀏覽器請求流程

2. Nginx控制瀏覽器緩存配置

Nginx經過添加Cache-Control(max-age)、Expires頭信息的方式控制瀏覽器緩存。

ngx_http_headers_module

語法

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 的符號。能夠在time值中使用正數或負數。
當 time 爲負數,「Cache-Control: no-cache」;
當 time 爲正數或0,「Cache-Control: max-age=time」,單位是秒。nginx

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」響應頭信息的添加或修改失效。正則表達式

3. 應用實例

1. vim /etc/nginx/conf.d/static.conf

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.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 17 Jul 2018 07:12:11 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "5b4d95aa-c"
Expires: Wed, 18 Jul 2018 07:12:11 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes

重點查看 ExpiresCache-Control兩個字段,可見,hello.txt 的緩存時間爲1天。vim

2、防盜鏈

目的:防止資源被盜用
思路:區別哪些請求是非正常的用戶請求

1. 基於http_refer防盜鏈配置模塊

ngx_http_referer_module

語法

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.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Fri, 03 Aug 2018 01:34:12 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "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.txt
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Fri, 03 Aug 2018 01:34:34 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
  • referer爲 http://192.168.1.110,能夠正常訪問
[root~]# curl -e "http://192.168.1.110" -I http://127.0.0.1/hello.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 02 Aug 2018 11:31:51 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "5b4d95aa-c"
Accept-Ranges: bytes
  • referer以 example.開頭或 .example.com 結尾,能夠正常訪問
[root~]# curl -e "http://www.example.com" -I http://127.0.0.1/hello.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 02 Aug 2018 11:33:47 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "5b4d95aa-c"
Accept-Ranges: bytes

[root~]# curl -e "http://example.baidu.com" -I http://127.0.0.1/hello.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 02 Aug 2018 11:33:53 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "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.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 02 Aug 2018 11:31:51 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "5b4d95aa-c"
Accept-Ranges: bytes
  • referer爲 http://google.com,返回403
[root~]# curl -e "http://google.com" -I http://127.0.0.1/hello.txt
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Thu, 02 Aug 2018 11:37:43 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
  • referer爲 http://www.google.com,能夠正常訪問
[root~]# curl -e "http://www.google.com" -I http://127.0.0.1/hello.txt
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Thu, 02 Aug 2018 11:37:50 GMT
Content-Type: text/plain
Content-Length: 12
Last-Modified: Tue, 17 Jul 2018 07:07:22 GMT
Connection: keep-alive
ETag: "5b4d95aa-c"
Accept-Ranges: bytes
相關文章
相關標籤/搜索