我正在使用 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";
}
}
道歉,因为我确信这是一些愚蠢的事情,但经过几个深夜后,我会很感激一些新鲜的眼睛来看待这个问题。
您以一种奇怪的方式在配置文件之间进行拆分。它对 nginx 没有任何影响,但它使人们阅读起来更加困难。
这是我将在某个时候发布的教程的合理记录工作配置 - 我怀疑 fastcgi_ignore_headers 可能会有所帮助。我在单个站点文件的顶部有这个,但它可以进入 nginx.conf。检查您的文件权限,我不认为是这样,但这对于任何清除缓存的进程都非常重要 - 这部分非常棘手。
这是我的位置块。请注意,我使用的是 Facebook 编写的 HHVM PHP 解释器,它比 PHP5 更快更高效,而 PHP7 不适用于我的 Wordpress 主题 Photocrati
然后在每个位置块内放置以下内容以帮助调试正在发生的事情 - 它会告诉您变量和正在使用的位置块,这将帮助您调试。使用 Firefox 和插件“Live HTTP Headers”查看输出