啟用FastCGI緩存
<br\>
編輯必須啟用緩存的虛擬主機配置文件。
- nano /etc/nginx/sites-enabled/vhost
將以下行添加到server{}指令之外的文件頂部:
- fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
- fastcgi_cache_key "$scheme$request_method$host$request_uri";
“fastcgi_cache_path”指令指定緩存(/etc/nginx/cache)的位置,其大小(100m),內存區域名稱(MYAPP),子目錄級別和非活動定時器。
位置可以在硬盤上的任何地方; 但是,大小必須小於您的服務器的RAM +交換,否則你會收到一個錯誤,“無法分配內存”。 如果緩存在“inactive”選項指定的特定時間內沒有被訪問(這里為60分鍾),Nginx將刪除它。
“fastcgi_cache_key”指令指定如何哈希緩存文件名。 Nginx基於此指令使用MD5加密訪問的文件。
在location ~ .php$ { }里添加如下行:
- fastcgi_cache MYAPP;
- fastcgi_cache_valid 200 60m;
“fastcgi_cache”指令引用我們在“fastcgicache_path”指令中指定的內存區域名稱,並將緩存存儲在此區域中。
默認情況下,Nginx根據這些響應頭里指定的時間決定存儲緩存對象的時間:X-Accel-Expires / Expires / Cache-Control。
如果缺少這些頭,“fastcgi_cache_valid”指令用於指定默認緩存生命周期。 在上面輸入的語句中,只緩存狀態代碼為200的響應。 也可以指定其他響應代碼。
測試配置:
- service nginx configtest
重載Nginx:
- service nginx reload
完整的vhost配置文件如下:
- fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
- fastcgi_cache_key "$scheme$request_method$host$request_uri";
- server {
- listen 80;
- root /usr/share/nginx/html;
- index index.php index.html index.htm;
- server_name example.com;
- location / {
- try_files $uri $uri/ /index.html;
- }
- location ~ \.php$ {
- try_files $uri =404;
- fastcgi_pass unix:/var/run/php5-fpm.sock;
- fastcgi_index index.php;
- include fastcgi_params;
- fastcgi_cache MYAPP;
- fastcgi_cache_valid 200 60m;
- }
- }
測試FastCGI緩存是否生效
<br\>
創建/usr/share/nginx/html/time.php,內容如下:
- <?php
- echo time();
- ?>
使用curl或您的Web瀏覽器多次請求此文件。
- root@droplet:~# curl http://localhost/time.php;echo
- 1382986152
- root@droplet:~# curl http://localhost/time.php;echo
- 1382986152
- root@droplet:~# curl http://localhost/time.php;echo
- 1382986152
如果緩存工作正常,您應該在緩存響應時在所有請求上看到相同的時間戳。
執行緩存位置的遞歸列表以查找此請求的緩存。
root@droplet:~# ls -lR /etc/nginx/cache/
/etc/nginx/cache/:
total 0
drwx—— 3 www-data www-data 60 Oct 28 18:53 e
/etc/nginx/cache/e:
total 0
drwx—— 2 www-data www-data 60 Oct 28 18:53 18
/etc/nginx/cache/e/18:
total 4
-rw——- 1 www-data www-data 117 Oct 28 18:53 b777c8adab3ec92cd43756226caf618e
我們還可以使Nginx為響應添加一個“X-Cache”頭,指示緩存是否被丟失或命中。
在server{}指令上面添加以下內容:
- add_header X-Cache $upstream_cache_status;
重新加載Nginx服務,並使用curl執行詳細請求以查看新標題。
root@droplet:~# curl -v http://localhost/time.php
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1…
* connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /time.php HTTP/1.1
> User-Agent: curl/7.26.0
> Host: localhost
> Accept: */*
>
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK < Server: nginx < Date: Tue, 29 Oct 2013 11:24:04 GMT < Content-Type: text/html < Transfer-Encoding: chunked < Connection: keep-alive < X-Cache: HIT < * Connection #0 to host localhost left intact 1383045828* Closing connection #0
不需要緩存的頁面
<br\>
某些動態內容(例如認證所需頁面)不應緩存。 可以基於諸如“requesturi”,“requestmethod”和“http_cookie”的服務器變量來排除這樣的內容被高速緩存。
如下例子:
- #Cache everything by default
- set $no_cache 0;
- #Don't cache POST requests
- if ($request_method = POST)
- {
- set $no_cache 1;
- }
- #Don't cache if the URL contains a query string
- if ($query_string != "")
- {
- set $no_cache 1;
- }
- #Don't cache the following URLs
- if ($request_uri ~* "/(administrator/|login.php)")
- {
- set $no_cache 1;
- }
- #Don't cache if there is a cookie called PHPSESSID
- if ($http_cookie = "PHPSESSID")
- {
- set $no_cache 1;
- }
要將“$no_cache”變量應用到相應的指令,請將以下行放在location〜.php $ {}中,
- fastcgi_cache_bypass $no_cache;
- fastcgi_no_cache $no_cache;
“fasctcgicachebypass”指令忽略之前由我們設置的條件相關的請求的現有緩存。 如果滿足指定的條件,“fastcginocache”指令不緩存請求。
清除緩存
<br\>
緩存的命名約定基於我們為“fastcgicachekey”指令設置的變量。
- fastcgi_cache_key "$scheme$request_method$host$request_uri";
根據這些變量,當我們請求“http//localhost/time.php”時,以下是實際值:
- fastcgi_cache_key "httpGETlocalhost/time.php";
將此字符串傳遞到MD5哈希將輸出以下字符串:
b777c8adab3ec92cd43756226caf618e
這就是高速緩存的文件名,就像我們輸入的“levels = 1:2”子目錄。 因此,目錄的第一級將從這個MD5字符串的最后一個字符命名為1個字符,即e; 第二級將具有在第一級之后的最后2個字符,即18.因此,該高速緩存的整個目錄結構如下:
/etc/nginx/cache/e/18/b777c8adab3ec92cd43756226caf618e
基於這種緩存命名格式,您可以用您最喜歡的語言開發一個清除腳本。 對於本教程,我將提供一個簡單的PHP腳本,它清除POSTed URL的緩存。
/usr/share/nginx/html/purge.php:
- <?php
- $cache_path = '/etc/nginx/cache/';
- $url = parse_url($_POST['url']);
- if(!$url)
- {
- echo 'Invalid URL entered';
- die();
- }
- $scheme = $url['scheme'];
- $host = $url['host'];
- $requesturi = $url['path'];
- $hash = md5($scheme.'GET'.$host.$requesturi);
- var_dump(unlink($cache_path . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash));
- ?>
向此文件發送帶需要清除的URL的POST請求。
- curl -d 'url=http://www.example.com/time.php' http://localhost/purge.php
該腳本將根據是否清除緩存而輸出true或false。 請確保從高速緩存中排除此腳本,並限制訪問。
