我正在运行带有 fpm php 5.5 的 Ubuntu nginx 1.8,在一个站点上我能够使缓存正常工作(带有简单的时间输出)<?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
从 wiki 中我尝试了不同的设置并压缩到以下单个文件(tester.com 可以工作,而 mywpsite.com 不行 - 这些是我在主机文件中设置的别名,肯定击中了正确的服务器)https://codex.wordpress.org/Nginx。
猫/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;
}
猫/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";
}
}
我很抱歉,因为我确信这是一件愚蠢的事情,但是经过几个深夜的努力之后,我希望能够用全新的视角来看待这件事。
答案1
你以一种奇怪的方式将内容拆分到配置文件中。这对 nginx 来说没什么影响,但会让阅读变得更加困难。
这是我为某个时候要发布的教程编写的相当好的工作配置 - 我怀疑 fastcgi_ignore_headers 可能会有帮助。我把它放在一个站点文件的顶部,但它可以进入 nginx.conf。检查你的文件权限,我不认为是那样,但对于任何清除缓存的过程来说,这确实很重要 - 那部分相当棘手。
# 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
}
然后在每个位置块中放入以下内容以帮助调试正在发生的事情 - 它将告诉您正在使用的变量和位置块,这将有助于您进行调试。使用 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