nginx不cachingwordpress网站

我有一个网站设置使用nginxcaching。 以下是详细信息:

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 default_server; listen [::]:80 default_server; set $no_cache 0; if ($request_method = POST) { set $no_cache 1; } if ($query_string != "") { set $no_cache 1; } if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $no_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $no_cache 1; } if ($http_cookie = "PHPSESSID") { set $no_cache 1; } root /var/www/websites/headstuff.org; index index.php; server_name _; location / { try_files $uri $uri/ /index.php?$args; } rewrite /wp-admin$ $scheme://$host$uri/ permanent; # Directives to send expires headers and turn off 404 error logging. 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 365d; #max; add_header Cache-Control "public"; } location ~ [^/]\.php(/|$) { try_files $uri $uri/ /index.php?q=$uri&$args; fastcgi_cache MYAPP; fastcgi_cache_valid 200 60m; fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply add_header X-Fastcgi-Cache $upstream_cache_status; fastcgi_cache_bypass $no_cache; fastcgi_no_cache $no_cache; # Regular PHP-FPM stuff: include fastcgi_params; fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 180; if (!-f $document_root$fastcgi_script_name) { return 404; } } } 

但是当我尝试

 curl -X GET -I headstuff.org 

我得到以下

 HTTP/1.1 301 Moved Permanently Date: Fri, 05 Feb 2016 10:48:22 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=d22f1949b207c61fa8f304a6be8d01e4a1454669298; expires=Sat, 04-Feb-17 10:48:18 GMT; path=/; domain=.headstuff.org; HttpOnly Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Expires: Thu, 19 Nov 1981 08:52:00 GMT Location: http://www.headstuff.org/ Pragma: no-cache Set-Cookie: PHPSESSID=03eubavpjg3ct5tsiu1g2eqi63; path=/ Vary: Cookie X-Fastcgi-Cache: MISS X-Pingback: http://www.headstuff.org/xmlrpc.php Server: cloudflare-nginx CF-RAY: 26fe0acb87752999-DUB 

总是说X-Fastcgi-Cache:MISS

任何想法发生了什么?

首先,您将nginxconfiguration为不caching301redirect响应。 如果你想caching这些,(你应该!)然后设置它:

 fastcgi_cache_valid 301 30d; 

其次,WordPress发送明确的指令,不cachingredirect,所以它不会被caching。

您指定了错误的url,请添加www。 当你这样做时,caching工作,你可以看到它的头。

将这些行添加到您的文件稍微改变你的caching。

 # Determines in which cases a stale cached response can be used when an error occurs during # communication with the FastCGI server fastcgi_cache_use_stale error timeout invalid_header http_500; # WordPress themes (especially Photocrati) often send back inappropriate headers, so ignore them fastcgi_ignore_headers Cache-Control Expires Set-Cookie; 

我也做了一堆头重写,因为我的主题确实很糟糕。