nginx 反代 Varnish 走 Https , WP 后台 ‘您没有足够的权限访问该页面。’

2016-07-10 20:24:42 +08:00
 ivmm

问题:

最近研究 Varnish 上 Https ,目前都是 nginx:443 反代 varnish:80 后端 nginx:80 。所以我也走的找个。 不过弄好以后。前台都是正常的,打开飞快,但是后台 wp-admin 什么地方都是 ‘您没有足够的权限访问该页面。’ WP 重新安装一遍也还是如此。

Nginx:443 的反代规则

location ~ / {
            proxy_pass http://127.0.0.1:80;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header X-SSL on;
            proxy_hide_header Vary;
            proxy_redirect off;
        }

Varnish 的规则

vcl 4.0;
 
# 后端服务器配置
backend default {
  .host = "127.0.0.1"; # 后端服务器的域名或 IP
  .port = "8080";                    # 端口
  .connect_timeout = 600s;
  .first_byte_timeout = 600s;
  .between_bytes_timeout = 600s;
  .max_connections = 128;
}
 
acl purge {
    "localhost";
    "127.0.0.1";
}
 
# vcl_recv 表示 Varnish 收到客户端请求的时候
sub vcl_recv {
  # 当 HTTP 方法是 Purge 时,检查来源 IP ,如果 IP 有效,则进行 Purge 操作
  if (req.method == "PURGE") {
    if (!client.ip ~ purge) {
      return(synth(405, "This IP is not allowed to send PURGE requests."));
    }
    return (purge);
  }
 
  # 不缓存有密码控制的内容和 Post 请求
  if (req.http.Authorization || req.method == "POST") {
    return (pass);
  }
 
  # 不缓存管理员页面和预览页面
  if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") {
    return (pass);
  }
 
  # 不缓存已登录用户的内容
  if (req.http.Cookie ~ "wordpress_logged_in_") {
    return (pass);
  }
 
  # 清除 cookie ,因为 WordPress 会根据用户 cookie 在评论框中直接输出昵称
  unset req.http.cookie;
 
  # 进行 hash 操作,见下面的定义
  return (hash);
}
 
sub vcl_pipe {
        return (pipe);
}
 
sub vcl_pass {
        return (fetch);
}
 
# 定义用于缓存的键
sub vcl_hash {
  # 这里使用 URL 做为键,如果是多域名站点,则需要使用 req.http.host + req.url
  hash_data(req.url);
  return (lookup);
}
 
# 处理后端服务器的响应
sub vcl_backend_response {
  # 删掉一些没有用的项
  unset beresp.http.X-Powered-By;
  unset beresp.http.x-mod-pagespeed;
 
  # 对于图片之类的静态内容,删掉 cookie 并且设置浏览器缓存时间为一个月
  if (bereq.url ~ "\.(css|js|png|gif|jp(e?)g|swf|ico|txt|eot|svg|woff)") {
    unset beresp.http.cookie;
    set beresp.http.cache-control = "public, max-age=2700000";
  }
 
  # 不缓存管理员页面和预览页面
  if (bereq.url ~ "wp-(login|admin)" || bereq.url ~ "preview=true") {
    set beresp.uncacheable = true;
    set beresp.ttl = 30s;
    return (deliver);
  }
 
  # 这一段很重要,在用户提交评论的同时,立即清空该页面的缓存,这样用户可以加载到最新的页面
  if (bereq.url == "/wp-comments-post.php") {
    ban("req.url == " + regsub(beresp.http.Location, "^http(s)?://bb\.mf8\.biz(/.*/)$
  }
 
  # 不缓存 Post 请求和有密码的内容
  if ( bereq.method == "POST" || bereq.http.Authorization ) {
    set beresp.uncacheable = true;
    set beresp.ttl = 120s;
    return (deliver);
  }
 
  # 只缓存正常的响应和 404
  if ( beresp.status != 200 && beresp.status != 404 ) {
    set beresp.uncacheable = true;
    set beresp.ttl = 120s;
    return (deliver);
  }
 
  unset beresp.http.set-cookie;
 
  # 默认缓存时间是 24 小时
  set beresp.ttl = 24h;
  set beresp.grace = 30s;
  return (deliver);
}
 
sub vcl_deliver {
  return (deliver);
}
sub vcl_init {
        return (ok);
}
sub vcl_fini {
        return (ok);
}
963 次点击
所在节点    问与答
12 条回复
ivmm
2016-07-11 13:58:51 +08:00
kn007
2016-07-11 15:22:39 +08:00
wp 你有处理么?

wp-config.php
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on';


# For wordpress
if (req.url ~ "/wp-(login|admin|cron|sign-in|data)" || req.http.Cookie ~ "wordpress_logged_in_" || req.http.Cookie ~ "comment_author_" || req.url ~ "(preview\=true|admin-ajax\.php)") {
# Don't cache, pass to backend
return (pass);
}
kn007
2016-07-11 15:23:51 +08:00
上面规则根据需要删减。 wp-data 和 wp-sign-in 是我构造的,评论不缓存,你也可以去掉。
ivmm
2016-07-11 15:27:20 +08:00
@kn007

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on';

有了

# For wordpress
if (req.url ~ "/wp-(login|admin|cron|sign-in|data)" || req.http.Cookie ~ "wordpress_logged_in_" || req.http.Cookie ~ "comment_author_" || req.url ~ "(preview\=true|admin-ajax\.php)") {
# Don't cache, pass to backend
return (pass);
}

添加在 sub vcl_recv { } 下重启后依旧没用
kn007
2016-07-11 15:29:44 +08:00
@ivmm 打开隐身窗口,登录观察下 cookie ,看下 wordpress_logged_in_是否正常。实在不行,改成 pipe 看下。
kn007
2016-07-11 15:30:09 +08:00
另外后端是 8080 吧,你此贴写成 80 了
ivmm
2016-07-11 15:31:30 +08:00
@kn007 现在改成了

Nginx:443 ——> Varnish:6081 ——> Nginx:8080
然后 Nginx:80 301 跳转到 Nginx:443 。

我看看 cookie 的部分
ivmm
2016-07-11 15:35:59 +08:00

ivmm
2016-07-11 15:37:42 +08:00
@kn007 cookie 是这里的么
kn007
2016-07-11 15:48:57 +08:00
@ivmm pipe 行不行呢? pipe 不行的话,切回 nginx443 到博客目录试试
ivmm
2016-07-11 15:56:30 +08:00
@kn007 # For wordpress
if (req.url ~ "/wp-(login|admin|cron)" || req.http.Cookie ~ "wordpress_logged_in_" || req.http.Cookie ~ "comment_author_" || req.url ~ "(preview\=true|admin-ajax\.php)") {
# Don't cache, pass to backend
return (pipe);
}

问题依旧
kn007
2016-07-11 16:09:03 +08:00
@ivmm Q 聊,还有那行 ban 是不是语法错了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/291552

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX