国产欧美日韩第一页|日本一二三不卡视频|在线精品小视频,亚洲第一免费播放区,metcn人体亚洲一区,亚洲精品午夜视频

幫助中心 >  技術(shù)知識庫 >  虛擬主機 >  虛擬主機基礎知識 >  Nginx使用教程(六):使用Nginx緩存之FastCGI緩存

Nginx使用教程(六):使用Nginx緩存之FastCGI緩存

2017-03-01 00:07:22 3780

Nginx使用教程(六):使用Nginx緩存之FastCGI緩存

啟用FastCGI緩存



編輯必須啟用緩存的虛擬主機配置文件。

  1. nano /etc/nginx/sites-enabled/vhost

將以下行添加到server{}指令之外的文件頂部:

  1. fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;

  2. fastcgi_cache_key "$scheme$request_method$host$request_uri";

“fastcgi_cache_path”指令指定緩存(/etc/nginx/cache)的位置,其大?。?00m),內存區域名稱(chēng)(MYAPP),子目錄級別和非活動(dòng)定時(shí)器。
位置可以在硬盤(pán)上的任何地方; 但是,大小必須小于您的服務(wù)器的RAM +交換,否則你會(huì )收到一個(gè)錯誤,“無(wú)法分配內存”。 如果緩存在“inactive”選項指定的特定時(shí)間內沒(méi)有被訪(fǎng)問(wèn)(這里為60分鐘),Nginx將刪除它。
“fastcgi_cache_key”指令指定如何哈希緩存文件名。 Nginx基于此指令使用MD5加密訪(fǎng)問(wèn)的文件。
在location ~ .php$ { }里添加如下行:

  1. fastcgi_cache MYAPP;

  2. fastcgi_cache_valid 200 60m;

“fastcgi_cache”指令引用我們在“fastcgicache_path”指令中指定的內存區域名稱(chēng),并將緩存存儲在此區域中。
默認情況下,Nginx根據這些響應頭里指定的時(shí)間決定存儲緩存對象的時(shí)間:X-Accel-Expires / Expires / Cache-Control。
如果?少這些頭,“fastcgi_cache_valid”指令用于指定默認緩存生命周期。 在上面輸入的語(yǔ)句中,只緩存狀態(tài)代碼為200的響應。 也可以指定其他響應代碼。
測試配置:

  1. service nginx configtest

重載Nginx:

  1. 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緩存是否生效



創(chuàng )建/usr/share/nginx/html/time.php,內容如下:

  1. <?php

  2. echo time();

  3. ?>

使用curl或您的Web瀏覽器多次請求此文件。

  1. root@droplet:~# curl http://www.tjdsmy.cn/time.php;echo

  2. 1382986152

  3. root@droplet:~# curl http://www.tjdsmy.cn/time.php;echo

  4. 1382986152

  5. root@droplet:~# curl http://www.tjdsmy.cn/time.php;echo

  6. 1382986152

如果緩存工作正常,您應該在緩存響應時(shí)在所有請求上看到相同的時(shí)間戳。
執行緩存位置的遞歸列表以查找此請求的緩存。
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為響應添加一個(gè)“X-Cache”頭,指示緩存是否被丟失或命中。
在server{}指令上面添加以下內容:

  1. add_header X-Cache $upstream_cache_status;

重新加載Nginx服務(wù),并使用curl執行詳細請求以查看新標題。
root@droplet:~# curl -v http://www.tjdsmy.cn/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

不需要緩存的頁(yè)面



某些動(dòng)態(tài)內容(例如認證所需頁(yè)面)不應緩存。 可以基于諸如“requesturi”,“requestmethod”和“http_cookie”的服務(wù)器變量來(lái)排除這樣的內容被高速緩存。
如下例子:

#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 $ {}中,

  1. fastcgi_cache_bypass $no_cache;

  2. fastcgi_no_cache $no_cache;

“fasctcgicachebypass”指令忽略之前由我們設置的條件相關(guān)的請求的現有緩存。 如果滿(mǎn)足指定的條件,“fastcginocache”指令不緩存請求。

清除緩存



緩存的命名約定基于我們?yōu)椤癴astcgicachekey”指令設置的變量。

  1. fastcgi_cache_key "$scheme$request_method$host$request_uri";

根據這些變量,當我們請求“http//localhost/time.php”時(shí),以下是實(shí)際值:

  1. fastcgi_cache_key "httpGETlocalhost/time.php";

將此字符串傳遞到MD5哈希將輸出以下字符串:
b777c8adab3ec92cd43756226caf618e
這就是高速緩存的文件名,就像我們輸入的“l(fā)evels = 1:2”子目錄。 因此,目錄的第一級將從這個(gè)MD5字符串的最后一個(gè)字符命名為1個(gè)字符,即e; 第二級將具有在第一級之后的最后2個(gè)字符,即18.因此,該高速緩存的整個(gè)目錄結構如下:
/etc/nginx/cache/e/18/b777c8adab3ec92cd43756226caf618e
基于這種緩存命名格式,您可以用您最喜歡的語(yǔ)言開(kāi)發(fā)一個(gè)清除腳本。 對于本教程,我將提供一個(gè)簡(jiǎn)單的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));
?>

向此文件發(fā)送帶需要清除的URL的POST請求。

  1. curl -d 'url=http://www.tjdsmy.cn/time.php' http://www.tjdsmy.cn/purge.php

該腳本將根據是否清除緩存而輸出true或false。 請確保從高速緩存中排除此腳本,并限制訪(fǎng)問(wèn)。


提交成功!非常感謝您的反饋,我們會(huì )繼續努力做到更好!

這條文檔是否有幫助解決問(wèn)題?

非常抱歉未能幫助到您。為了給您提供更好的服務(wù),我們很需要您進(jìn)一步的反饋信息:

在文檔使用中是否遇到以下問(wèn)題:
-->