Nginx的fastcgi_cache不工作的一个WP网站(但工作正常time.php在不同的虚拟主机)

我运行的Ubuntu nginx 1.8与FPM PHP 5.5,在一个网站上我能够得到caching工作(与一个简单的时间输出) <?php echo time();?>显示:

 Connection:keep-alive Content-Encoding:gzip Content-Type:text/html Date:Fri, 08 Jan 2016 14:04:00 GMT Fastcgi-Cache:HIT Server:nginx/1.8.0 Transfer-Encoding:chunked X-Powered-By:PHP/5.5.9-1ubuntu4.14 

但是对于WordPress站点来说,标题总是显示(甚至在注销和隐身时):

 Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection:keep-alive Content-Encoding:gzip Content-Type:text/html; charset=UTF-8 Date:Fri, 08 Jan 2016 14:02:42 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Fastcgi-Cache:BYPASS Link:<http://mywpsite.com/wp-json/>; rel="https://api.w.org/" Link:<http://mywpsite.com/>; rel=shortlink Pragma:no-cache Server:nginx/1.8.0 Transfer-Encoding:chunked Vary:Accept-Encoding X-Powered-By:PHP/5.5.9-1ubuntu4.14 

从维基我已经尝试不同的设置,并压缩到以下单个文件(tester.com工程,而mywpsite.com不 – 这些都是我已经在我的主机文件中设置的别名,并肯定击中正确的服务器) https:// codex .wordpress.org / Nginx 。

cat /etc/nginx/nginx.conf

 user www-data; worker_processes 4; pid /run/nginx.pid; events { worker_connections 1024; # multi_accept on; } http { fastcgi_cache_key "$scheme$request_method$host$request_uri"; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; gzip on; gzip_disable "msie6"; application/xml+rss text/javascript; # Upstream to abstract backend connection(s) for PHP. upstream php { #this should match value of "listen" directive in php-fpm pool server unix:/var/run/php5-fpm.sock; } include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; add_header Fastcgi-Cache $upstream_cache_status; } 

cat /etc/nginx/sites-available/tester.com

 fastcgi_cache_path /home/tester.com/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m; server { listen 80; server_name tester.com; root /home/tester.com/public_html; index index.php index.html index.htm; 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; } } 

猫/etc/nginx/sites-available/mywpsite.com

 fastcgi_cache_path /home/mywpsite.com/cache levels=1:2 keys_zone=MYWPSITE:100m inactive=60m; server { server_name mywpsite.com; root /home/mywpsite.com/public_html; index index.php; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location ~ /\. { access_log off; log_not_found off; deny all; } location ~* /(?:uploads|files)/.*\.php$ { deny all; } location / { try_files $uri $uri/ /index.php?$args; } # Add trailing slash to */wp-admin requests. rewrite /wp-admin$ $scheme://$host$uri/ permanent; set $skip_cache 0; if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 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; } fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache MYWPSITE; fastcgi_cache_valid 60m; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default) include fastcgi.conf; fastcgi_index index.php; # fastcgi_intercept_errors on; fastcgi_pass php; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache MYWPSITE; fastcgi_cache_valid 200 60m; } location ~ /purge(/.*) { fastcgi_cache_purge MYWPSITE"$scheme$request_method$host$1"; } } 

道歉,我相信这是愚蠢的,但经过了一些深夜,会欣赏一些新鲜的眼睛在这个过程。

你有一些奇怪的事情在你的configuration文件之间分割。 这对nginx来说没有什么区别,但是让人阅读起来更加困难。

这里是我有一个合理的logging工作configuration的教程,我将在某个时候发布 – 我怀疑fastcgi_ignore_headers可能会有所帮助。 我有一个单一的网站文件的顶部,但它可以进入nginx.conf。 检查你的文件权限,我不认为是这样,但是对于任何清除caching的进程来说都是非常重要的 – 这个部分是非常棘手的。

 # Caching. Putting the cache into /dev/shm keeps it in RAM, limited to 10MB, for one day. Or put it into a more standard /etc/nginx/cache # You can move to disk if you like, or extend the caching time fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=WORDPRESS:10m inactive=1440m; #Disk #fastcgi_cache_path /dev/shm/nginxcache levels=1:2 keys_zone=WORDPRESS:10m inactive=1440m; #RAM # This is to do with how the files are laid out on disk fastcgi_cache_key "$scheme$request_method$host$request_uri"; # 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 ; # Rules to work out when cache should/shouldn't be used 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; } 

这是我的位置块。 请注意,我使用由Facebook编写的HHVM PHP解释器,它比PHP5更快,更高效,PHP7不支持我的Wordpress主题Photocrati

  # Send HipHop and PHP requests to HHVM location ~ \.(hh|php)$ { fastcgi_keep_conn on; fastcgi_intercept_errors on; fastcgi_pass php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # Use the cache defined above. Cache only 200 (success) status's, for 24 hours # Only cache GET and HEAD requests fastcgi_cache WORDPRESS; fastcgi_cache_valid 200 1440m; add_header X-Cache $upstream_cache_status; fastcgi_cache_methods GET HEAD; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; # Clear the server ID, for security. Clear the cache control headers. more_clear_headers Server; more_clear_headers "Pragma"; # Add cache control headers that say each page is valid for an hour add_header Z_LOCATION "PHP MAIN"; add_header URI $uri; # DEBUG } 

然后在每个位置块内放置以下内容以帮助debugging正在发生的事情 – 它会告诉您正在使用的variables和位置块,这将帮助您进行debugging。 使用Firefox和插件“Live HTTP Headers”来查看输出

 add_header Z_LOCATION "(name of your location block)"; # DEBUG add_header Z_URI $uri; # DEBUG add_header Z_CACHE $skip_cache; # DEBUG add_header URI $uri; # DEBUG