是否可以将nginx配置为仅忽略某些角色中已登录用户的缓存?

时间:2017-02-21 作者:Stephen

我有一个忽略登录用户的nginx缓存设置,但我有很多用户在订阅服务器角色中,他们应该被服务于一个大部分缓存的站点。是否有一种方法可以只忽略某些角色(如contributor和更高级别)的缓存,而保留订阅者(以及某些时候可能的其他角色)的缓存?

1 个回复
最合适的回答,由SO网友:Stephen 整理而成

下面是我最后做的:

// Set disable cache for certain roles
add_action(\'init\', \'add_custom_cookie_admin\');
function add_custom_cookie_admin() {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$thisrole = $current_user->roles[0];
  if($thisrole !== \'subscriber\') {
    setcookie("disable_cache", $current_user->user_login, time()+43200, COOKIEPATH, COOKIE_DOMAIN);
  }
  }
}
// and then remove the cookie on logout
function clear_custom_cookie_on_logout() {
    unset($_COOKIE["disable_cache"]);
    setcookie( "disable_cache", \'\', time() - ( 15 * 60 ) );
}
add_action(\'wp_logout\', \'clear_custom_cookie_on_logout\');
然后我将其添加到我的nginx缓存中:

if ($http_cookie ~* "disable_cache") {
set $skip_cache 1;
}