我现在有一个完整的Nginx安装,使用PHP-FPM。 来自Apache前面的Nginx代理的世界。
看来Nginx有自己喜怒无常的caching,非常具有攻击性。 有几层caching:
Nginx的fastcgicaching本身。 在我的nginx.conf中,我有以下设置:
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
那里有PHP的opcache。 我现在禁用了它:
;zend_extension=opcache.so opcache.enable=0 opcache.enable_cli=0 opcache.memory_consumption=250 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=6000 opcache.revalidate_freq=600 opcache.fast_shutdown=1
我在服务器块中也有以下skip cache指令:
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 the following URLs if ($request_uri ~* "/(wp-login.php|wp-admin|login.php|backend|admin)"){ set $skip_cache 1; } #Don't cache if there is a cookie called PHPSESSID if ($http_cookie ~* "PHPSESSID"){ set $skip_cache 1; } #Don't cache if there is a cookie called wordpress_logged_in_[hash] if ($http_cookie ~* "wordpress_logged_in_"){ set $skip_cache 1; } # Don't cache uris containing the following segments if ($request_uri ~* "cms/|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; } # For the arc if ($request_uri ~* "site/|/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; }
然后在PHP块中,我使用skip_cache的东西:
location ~ \.php$ { try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache WORDPRESS; fastcgi_cache_valid 200 301 302 60m; }
尽pipe如此,我的pipe理员屏幕非常积极地被caching,当我激活一个插件时,它显示了较旧的屏幕。 当插件被停用。 只有当我手动刷新页面,我看到插件确实被激活。
我错过了什么?
这一点的configuration看起来很熟悉。 我想这可能来自WordPress维基。 无论如何,这种方式过于复杂,而且通常是不必要的。
我要从一个活的WordPress网站拉我的configuration:
set $fastcgi_nocache ""; if ($http_cookie ~ (comment_author_.*|wordpress_logged_in.*|wp-postpass_.*)) { set $fastcgi_nocache "true"; } fastcgi_ignore_headers Expires Cache-Control; fastcgi_hide_header Pragma; fastcgi_cache_bypass $fastcgi_nocache; fastcgi_no_cache $fastcgi_nocache;
就这样。 你唯一需要检查的是用户是否有一个cookie,表示他已经login,留下了一条评论,想要被记住,或者input了一个密码。 其他的一切都不重要或不适用于WordPress。
迈克尔汉普顿build议的方式是恕我直言,最好(最简单,最有效),但是这里有一个替代/附加的方法,可能在某些情况下是有用的。 它来自我的Wordpress / Nginx教程 。 有些主题与caching标题混淆在一个非常糟糕的方式,所以我基本上要重写所有不同的网站的不同领域的标题。 我也删除旧的Pragma标题。
我为特定的URL或子目录定义块,这样我就可以更精确地限制或控制标题。
请注意,“more_clear_headers”和“add_header”是Headers More Nginx扩展的一部分 。 如果你看看我上面链接的教程,第二部分,它提供了一步一步的说明如何使用该模块构buildNginx – 这很容易。
# Rate limiting for login pages limit_req_zone $binary_remote_addr zone=login:1m rate=5r/m; # Rate limit wp-login.php to help prevent brute force attacks location = /blog/wp-login.php { # Next line applies the rate limit defined above limit_req zone=login burst=3; fastcgi_keep_conn on; fastcgi_intercept_errors on; fastcgi_pass php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; more_clear_headers "Cache-Control"; more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires"; # No caching more_clear_headers "Cache-Control"; add_header Cache-Control "private, max-age=0, no-cache, no-store"; more_clear_headers "Expires"; # Debugging aid - remove # add_header Z_LOCATION "WP-LOGIN"; add_header URI $uri; } # WordPress admin caching headers are generally set correctly, for pages and resources. The only reason we define # this block separately is to avoid messing with the headers in the main php block. # This is probably unnecessary because of the skip_cache variable and may be removed location ~* wp-admin { fastcgi_keep_conn on; fastcgi_intercept_errors on; fastcgi_pass php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # Debugging aid - remove # add_header Z_LOCATION "WP_ADMIN"; add_header URI $uri; add_header "Z_SKIP_CACHE" $skip_cache; }
要添加其他的东西来添加到迈克尔的答案,我也使用这个块。 它以第二种方式禁止Wordpresspipe理员的caching,同时也禁用了feed,sitemap,xmlrpc和其他一些caching。 它的一部分是为WordPress,我怀疑它的一部分是我运行的自定义书面的PHP网站。
if ($request_uri ~* "/wp-admin/|/admin-*|/purge*|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml|wp-cron") { set $fastcgi_nocache "true"; }