我想在我的nginx 1.5.8安装程序上尝试fastcgi_cache ,如下所示。
在nginx conf中,http部分,我补充道:
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:10m max_size=1000m inactive=60m;
在服务器部分:
set $cache_uri $request_uri; # POST requests and urls with a query string should always go to PHP if ($request_method = POST) { set $cache_uri 'null cache'; } if ($query_string != "") { set $cache_uri 'null cache'; } # Don't cache uris containing the following segments if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $cache_uri 'null cache'; } # Don't use the cache for logged in users or recent commenters if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $cache_uri 'null cache'; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; ## # Fastcgi cache ## set $skip_cache 1; if ($cache_uri != "null cache") { add_header X-Cache-Debug "$cache_uri $cookie_nocache $arg_nocache$arg_comment $http_pragma $http_authorization"; set $skip_cache 0; } fastcgi_cache_bypass $skip_cache; fastcgi_cache_key $scheme$host$request_uri$request_method; fastcgi_cache_valid any 8m; fastcgi_cache_bypass $http_pragma; fastcgi_cache_use_stale updating error timeout invalid_header http_500; }
我把/var/cache/nginx为www-data用户(和组), chmod把它指定为775 。 我重新启动了nginx,但文件夹总是空的。 这是正常的吗? 我如何testingfastcgi_cache是否工作?
您还需要设置fastcgi_cache microcache; 在你的configuration的某个点。 默认值null禁用caching: http : //nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache
为了检查它,看看X-Cache-Debug头是否出现在响应中。 如果不是这样,那么对于可以caching的内容太过于限制。 Nginx也支持您在HTTP响应中设置的Cache-Control头 – 您可以在应用程序中使用/检查它,而不是为caching添加太多的条件。
是的,这很正常。
在你的configuration中没有你有fastcgi_cache指令。
如果没有指定,隐含的值是off 。 如果你想要进行任何caching,最好提一下fastcgi_cache microcache; 在某处, keys_zone部分是来自fastcgi_cache_path的keys_zone的名称。