【nginx】關於fastcgi_cache


一、簡介

Nginx版本從0.7.48開始,支持了類似Squid的緩存功能。這個緩存是把URL及相關組合當做Key,用Md5算法對Key進行哈希,得到硬盤上對應的哈希目錄路徑,從而將緩存內容保存在該目錄內。

Nginx Web 緩存服務只能為指定URL或狀態碼設置過期時間,不支持類似Squid的PURGE指令手動清除緩存;但是我們可以通過Nginx的模塊ngx_cache_purge清除指定URL的緩存。

  • proxy_cache:緩存后端服務器的內容,可能是任何內容,包括靜態的和動態,減少了nginx與后端通信的次數,節省了傳輸時間和后端寬帶
  • fastcgi_cache:緩存fastcgi生成的內容,很多情況是php生成的動態的內容,少了nginx與php的通信的次數,更減輕了php和數據庫(mysql)的壓力,這比用memcached之類的緩存要輕松得多


圖片來自網絡

二、配置

nginx.conf

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_temp_path  /tmp/nginx/fcgi/temp;

vhost配置

server {
	server_name example.com www.example.com;

	access_log   /var/log/nginx/example.com.access.log;
	error_log    /var/log/nginx/example.com.error.log;

	root /var/www/example.com/htdocs;
	index index.php;

	set $skip_cache 0;

	# POST requests and urls with a query string should always go to PHP
	if ($request_method = POST) {
		set $skip_cache 1;
	}   
	if ($query_string != "") {
		set $skip_cache 1;
	}   

	# Don't cache uris containing the following segments
	if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
		set $skip_cache 1;
	}   

	# Don't use the cache for logged in users or recent commenters
	if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
		set $skip_cache 1;
	}

	location / {
		try_files $uri $uri/ /index.php?$args;
	}    

	location ~ \.php($|/) {
		try_files $uri =404; 
		include fastcgi_params;
		fastcgi_split_path_info         ^(.+\.php)(/.+)$;
		fastcgi_param PATH_INFO         $fastcgi_path_info;
		fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
		fastcgi_pass unix:/dev/shm/php-socket;

		fastcgi_cache_bypass $skip_cache;
		fastcgi_no_cache $skip_cache;

		fastcgi_cache WORDPRESS;
		include fcgi_cache_params;

	}

	location ~ /purge(/.*) {
	    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
	}	

	location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
		access_log off;	log_not_found off; expires max;
	}

	location = /robots.txt { access_log off; log_not_found off; }
	location ~ /\. { deny  all; access_log off; log_not_found off; }
}

fcgi_cache_params配置

#include fcgi_cache_params;
#fastcgi_cache_valid 200 302 1s;
### fcgi-cache
fastcgi_cache fcgi;
fastcgi_cache_valid 200 302 1s;
fastcgi_cache_valid 404 500 502 503 504 0s;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500 http_503 updating;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#add_header X-Cache "$upstream_cache_status - $upstream_response_time";
fastcgi_cache_key "$scheme$request_method$host$request_uri"

大概解釋下各個參數的含義:

fastcgi_cache  該指令用於設置哪個緩存區將被使用,zone_name的值為fastcgi_cache_path指令創建的緩存名稱

fastcgi_cache_path 作用域:http

fastcgi_cache_path path 
[levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] 
[max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time] 
[purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

該指令用於設置緩存文件的存放路徑,示例如下:fastcgi_cache_path /data/nginx/cache levels=1:2  keys_zone=cache_one:100M  inactive=1d  max_size=10g; 

a、levels:指定了該緩存空間有兩層hash目錄,設置緩存目錄層數,levels=1:2,表示創建兩層目錄緩存,最多創建三層。第一層目錄名取fastcgi_cache_key md5的最后一個字符,第二層目錄名取倒數2-3字符,如:fastcgi_cache_key md5為b7f54b2df7773722d382f4809d65029c,則:

levels=1:2為/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
levels=1:2:3為/data/nginx/cache/c/29/650/b7f54b2df7773722d382f4809d65029c

b、keys_zone為這個緩存區起名為zone_name,500m指代緩存空間為500MB; 

c、inactive=1d 代表如果緩存文件一天內沒有被訪問,則刪除; 

d、max_size=30g代表硬盤緩存最大為30G;

設置緩存多個磁盤

fastcgi_cache_path /path/to/hdd1 levels=1:2 keys_zone=my_cache_hdd1:10m max_size=10g  inactive=60m use_temp_path=off;
fastcgi_cache_path /path/to/hdd2 levels=1:2 keys_zone=my_cache_hdd2:10m max_size=10g  inactive=60m use_temp_path=off;

split_clients $request_uri $my_cache {
              50%          "my_cache_hdd1";
              50%          "my_cache_hdd2";
}

server {
    ...
    location / {
       fastcgi_cache $my_cache;
       
    }
}

將緩存文件放入內存中

編輯/etc/fstab  或者 放入 /dev/shm

tmpfs /etc/nginx/cache tmpfs defaults,size=100M 0 0
mount -a
df -ah | grep tmpfs 

需要注意的是fastcgi_cache緩存是先寫在fastcgi_temp_path再移到fastcgi_cache_path,所以這兩個目錄最好在同一個分區,從0.8.9之后可以在不同的分區,不過還是建議放同一分區

 

fastcgi_cache_methods 該指令用於設置緩存哪些HTTP方法,默認緩存HTTP GET/HEAD方法。 

fastcgi_cache_min_uses  URL經過多少次請求將被緩存

fastcgi_cache_valid  reply_code [reply_code ... ] time

該指令用於對不同返回狀態碼的URL設置不同的緩存時間,例如: 

fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404      1m;

設置202 302狀態URL緩存10分鍾,404狀態的URL緩存1分鍾。

注意:如果不指定狀態碼,直接指定緩存時間,則只有200,301,302狀態碼會進行緩存。

fastcgi_cache_valid 5m;

any 可以指定緩存任何響應碼    

fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301      1h;
fastcgi_cache_valid any      1m;

緩存的參數也可以在響應頭直接設置。這些的優先級高於緩存時間設定使用該指令

  • The “X-Accel-Expires” header field sets caching time of a response in seconds. The zero value disables caching for a response. If the value starts with the @ prefix, it sets an absolute time in seconds since Epoch, up to which the response may be cached.
  • If the header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or “Cache-Control”.
  • If the header includes the “Set-Cookie” field, such a response will not be cached.
  • If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes the “Vary” field with another value, such a response will be cached taking into account the corresponding request header fields (1.7.7).

fastcgi_cache_key

該指令用來設置Web緩存的Key值,Nginx根據Key值MD5緩存。一般根據$host(域名),$request_uri(請求的路徑)等變量組合成fastcgi_cache_key。

例如:fastcgi_cache_key "$scheme$request_method$host$request_uri";

定義fastcgi_cache的key,示例中就以請求的URI作為緩存的key,Nginx會取這個key的md5作為緩存文件,如果設置了緩存哈希目錄,Nginx會從后往前取相應的位數做為目錄。

注意一定要加上$request_method作為cache key,否則如果HEAD類型的先請求會導致后面的GET請求返回為空

fastcgi_temp_path  path [level1 [level2 [level3]]];  默認為 fastcgi_temp;

該指令用來設置fastcgi_cache臨時文件目錄

fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;

a temporary file might look like this:

/spool/nginx/fastcgi_temp/7/45/00000123457

fastcgi_cache_use_stale : fastcgi_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_503 | http_403 | http_404 | off ...;

定義哪些情況下用過期緩存

x-cache頭,用於調試

$upstream_response_time為過期時間

$upstream_cache_status 變量表示此請求響應來自cache的狀態,幾種狀態分別為:

  • MISS – The response was not found in the cache and so was fetched from an origin server. The response might then have been cached.
  • BYPASS – The response was fetched from the origin server instead of served from the cache because the request matched a proxy_cache_bypass directive (see Can I Punch a Hole Through My Cache? below.) The response might then have been cached.
  • EXPIRED – The entry in the cache has expired. The response contains fresh content from the origin server.
  • STALE – The content is stale because the origin server is not responding correctly, and proxy_cache_use_stale was configured.
  • UPDATING – The content is stale because the entry is currently being updated in response to a previous request, and proxy_cache_use_stale updating is configured.
  • REVALIDATED – The proxy_cache_revalidate directive was enabled and NGINX verified that the current cached content was still valid (If-Modified-Since or If-None-Match).
  • HIT – The response contains valid, fresh content direct from the cache.

有一些情況會影響到cache的命中 這里需要特別注意

  • Nginx fastcgi_cache在緩存后端fastcgi響應時,當響應里包含“set-cookie”時,不緩存;
  • 當響應頭包含Expires時,如果過期時間大於當前服務器時間,則nginx_cache會緩存該響應,否則,則不緩存;
  • 當響應頭包含Cache-Control時,如果Cache-Control參數值為no-cache、no-store、private中任意一個時,則不緩存,如果Cache-Control參數值為max-age時,會被緩存,且nginx設置的cache的過期時間,就是系統當前時間 + mag-age的值。
header("Expires: ".gmdate("D, d M Y H:i:s", time()+10000).' GMT');
header("Expires: ".gmdate("D, d M Y H:i:s", time()-99999).' GMT');
header("X-Accel-Expires:5"); // 5s
header("Cache-Control: no-cache"); //no cache
header("Cache-Control: no-store"); //no cache
header("Cache-Control: private"); //no cache
header("Cache-Control: max-age=10"); //cache 10s
setcookie('hello',"testaaaa"); //no cache

注意session使用的時候有坑,可以用下面來設置

session_cache_limiter("none");
session_start();
echo date("Y-m-d H:i:s",time());

可以看一下PHP源代碼中的頭信息 Expires等

//ext/session/session.c  line:1190 左右
// ...
CACHE_LIMITER_FUNC(private) /* {{{ */
{
    ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
    CACHE_LIMITER(private_no_expire)(TSRMLS_C);
}
/* }}} */
//再到這里3 或者上面幾個 ##默認是nocache
CACHE_LIMITER_FUNC(nocache) /* {{{ */
{
    ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
 
    /* For HTTP/1.1 conforming clients and the rest (MSIE 5) */
    ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
 
    /* For HTTP/1.0 conforming clients */
    ADD_HEADER("Pragma: no-cache");
}
/* }}} */
//這里2
static php_session_cache_limiter_t php_session_cache_limiters[] = {
    CACHE_LIMITER_ENTRY(public)
    CACHE_LIMITER_ENTRY(private)
    CACHE_LIMITER_ENTRY(private_no_expire)
    CACHE_LIMITER_ENTRY(nocache)
    {0}
};
 
static int php_session_cache_limiter(TSRMLS_D) /* {{{ */
{
    php_session_cache_limiter_t *lim;
 
    if (PS(cache_limiter)[0] == '\0') return 0;
 
    if (SG(headers_sent)) {
        const char *output_start_filename = php_output_get_start_filename(TSRMLS_C);
        int output_start_lineno = php_output_get_start_lineno(TSRMLS_C);
 
        if (output_start_filename) {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cache limiter - headers already sent (output started at %s:%d)", output_start_filename, output_start_lineno);
        } else {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot send session cache limiter - headers already sent");
        }
        return -2;
    }
 
    for (lim = php_session_cache_limiters; lim->name; lim++) {
        if (!strcasecmp(lim->name, PS(cache_limiter))) {
            lim->func(TSRMLS_C);   //這里1
            return 0;
        }
    }
 
    return -1;
}

三、清除緩存

NGINX只在商業版中支持proxy_cache_purge指令清除緩存,開源的ngx_cache_purge模塊只支持單一key的緩存清除。為了實現按目錄清除緩存只能自己開發。

NGINX作為Cache服務器時將資源內容以文件形式進行緩存,緩存元信息存儲於共享內存中,組織成一棵紅黑樹。紅黑樹中的每個節點代表一個Cache元信息。NGINX將Cache Key的HASH值作為紅黑樹節點的KEY。內容緩存文件以該HASH值作為文件名存儲在磁盤上。

NGINX的處理流程簡化描述是這樣的:當請求到達時,根據Cache Key的HASH值在紅黑樹中進行查找。如果找到,並查看相關信息,如果Cache可用,返回相應的Cache文件。否則,則回源抓取。

因為元信息是以Cache Key的HASH值作為Key存儲的,因而紅黑樹中並不能保留Cache Key中有層級關系. 如”/uri/foo”和”/uri/bar”在元信息紅黑樹中完全沒有關系。要實現按照目錄清除緩存,需要將Cache Key中層次關系存儲起來。

可以這樣做,在共享內存中建立一棵目錄樹來存儲層級關系。將Cache Key類比於文件系統中的路徑, 每級路徑存儲為樹中的一個節點。當需要清除某一目錄下的所有緩存時,將該節點子樹的中的所有緩存清除即可。

安裝Purge模塊

Purge模塊被用來清除緩存

wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
tar -zxvf ngx_cache_purge-1.2.tar.gz

編譯

./configure \
…… \
--with-http_geoip_module \
--add-module=/usr/local/ngx_cache_purge-1.2

四、需要注意的一些問題

設置了之后重啟nginx就可以生效了,這個時候再訪問php的頁面的話,就會被緩存了,可以查看/var/logs/nginx/fastcgi_cache_dir這個目錄下面是有緩存文件的。最后再說明一點,如果更改了緩存目錄的路徑,一定要把緩存的名稱也改掉,后端調用的名稱也同步改掉,如果只改掉了緩存目錄,不改緩存名稱的話,緩存的時候還是會緩存到之前的路徑下面去,但是調用的時候調用的是新的路徑,這個時候就會出現找不到的情況

 

參考文章

http://www.nginxtips.com/configure-nginx-fastcgi-cache/

http://www.haidx.com/fastcgi-cache-details.html

http://www.just4coding.com/blog/2014/11/01/nginx-purge-directory/

http://weizhifeng.net/nginx-proxy-cache.html

https://www.nginx.com/blog/nginx-caching-guide/#gs.6PdbraI

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache

https://www.cnxct.com/several-reminder-in-nginx-fastcgi_cache-and-php-session_cache_limiter/

https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM