我正在尝试为WordPress的博客做反向代理caching,我碰到了一些麻烦。
初始configuration是这样一个简单的反向caching
location / { proxy_cache_key "$scheme://$host$request_uri"; proxy_cache staticfilecache; proxy_pass http://wordpressapache; add_header Cache-Control public; proxy_cache_valid 200 302 10d;
我很快意识到,login的用户将被发送与折腾,所以我做了
proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Accept-Encoding ""; location / { # If logged in, don't cache. if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) { set $do_not_cache 1; } proxy_cache_key "$scheme://$host$request_uri $do_not_cache"; proxy_cache staticfilecache; proxy_pass http://wordpressapache; add_header Cache-Control public; proxy_cache_valid 200 302 10d; }
但是这不起作用,因为Nginx 1.0+不会caching具有Set-Cookie的响应 。 令我惊奇的是,即使所有的匿名用户命中都会导致Set-Cookie 。 下面是一个例子。

现在我不能做了
proxy_ignore_headers "Set-Cookie"; proxy_hide_header "Set-Cookie";
因为这也不会为login用户传递cookie。 我做了这样的事情
location ~* wp\-.*\.php|wp\-admin { proxy_pass http://wordpressapache; }
但是,login的用户也将回到博客页面,他们的cookies将被拒绝。
另一种方法是通过插件做这样的事情,并发送X-Accel-Expires。
function add_xaccel_header() { # Set the X-Accel-Expires header to never cache the page if it looks like the page needs to be tailored for a user. $user_cookie_there = false; foreach($_COOKIE as $key => $value){ if( preg_match('/wordpress_(?!test_cookie)|comment_author|wp-postpass/', $key) ){ $user_cookie_there = true; } } if($user_cookie_there){ header("X-Accel-Expires: 0"); } } add_action('init','add_xaccel_header');
但是,隐藏cookie正在创造问题。 删除隐藏cookie也打破了caching。
caching使用Nginx的匿名用户页面的策略是什么,login用户可以使其失效。 也许使用proxy_cache_bypass ?