我已经在 WordPress 中设置了带有缓存的 nginx。
我正在使用https://wordpress.org/plugins/nginx-helper/,但是登录页面似乎被缓存了,而它不应该被缓存(因此显示登录栏)。
一些未登录(也没有任何 cookies/浏览器缓存)的访问者会看到登录栏(如果他们点击栏内的任何内容,它会将他们重定向到登录页面)。
此外,当我单击 WordPress 中的清除缓存按钮时,页面仍然显示为XCache HIT
...真正清除的唯一方法是通过rm -rf /dev/shm/nginx
。
我确实安装了以下模块:
nginx version: nginx/1.8.0
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log
--error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi
--with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module
--with-http_stub_status_module --with-http_realip_module
--with-http_auth_request_module --with-http_addition_module
--with-http_geoip_module --with-http_gzip_static_module
--with-http_image_filter_module --with-http_spdy_module
--with-http_sub_module --with-http_xslt_module
--add-module=/build/buildd/nginx-1.8.0/debian/modules/headers-more-nginx-module
--add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-auth-pam
--add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-cache-purge
--add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-echo
--add-module=/build/buildd/nginx-1.8.0/debian/modules/ngx-fancyindex
--add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-lua
--add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-upload-progress
--add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-upstream-fair
--add-module=/build/buildd/nginx-1.8.0/debian/modules/ngx_http_substitutions_filter_module
--add-module=/build/buildd/nginx-1.8.0/debian/modules/ngx_pagespeed
请问我的配置有什么问题吗?
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
geoip_country /usr/local/share/GeoIP/GeoIP.dat;
geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 300m;
proxy_send_timeout 300;
proxy_read_timeout 300;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
# Upstream to abstract backend connection(s) for PHP.
upstream php {
server 127.0.0.1:9000;
}
include /etc/nginx/conf.d/*.conf;
fastcgi_cache_path /dev/shm/nginx levels=1:2 keys_zone=WPCACHE:384m max_size=3072m inactive=480m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
server_name mysite.com;
root /home/mysite/public_html/;
index index.php;
access_log /home/mysite/logs/access.log;
error_log /home/mysite/logs/error.log;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
# Rules to work out when cache should or should not 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;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
fastcgi_cache WPCACHE;
fastcgi_cache_valid 200 480m;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
#more_clear_headers Server; more_clear_headers "Pragma";
add_header Z_LOCATION "PHP MAIN"; add_header URI $uri; # DEBUG
add_header X-Cache $upstream_cache_status;
}
location ~ /purge(/.*) {
fastcgi_cache_purge WPCACHE "$scheme$request_method$host$1";
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|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;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
}
答案1
该配置看起来非常熟悉 - 我将其大部分内容发布在另一个帖子中:)可惜它缺少一个重要部分。
location = /wp-login.php {
fastcgi_keep_conn on;
fastcgi_intercept_errors on;
fastcgi_pass php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# No caching
more_clear_headers "Cache-Control";
add_header Cache-Control "private, max-age=0, no-cache, no-store";
more_clear_headers "Expires";
}
此外,Nginx Helper 无法清除缓存。我花了很长时间尝试让它工作,但就是不行。我无法获得Nginx 缓存插件也可以工作。
Nginx 缓存更新
来自评论中的@rafa:问题是 nginx-helper 的缓存文件路径硬编码为 /var/run/nginx-cache,而 OP 的配置指示缓存文件的路径为 /dev/shm/nginx。我只需将 fastcgi_cache_path 更改为“正确”路径,nginx-cache 就可以正常工作