V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ivmm
V2EX  ›  问与答

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

  •  
  •   ivmm · 2016-07-10 20:24:42 +08:00 · 958 次点击
    这是一个创建于 2868 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题:

    最近研究 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);
    }
    
    12 条回复    2016-07-11 16:09:03 +08:00
    ivmm
        1
    ivmm  
    OP
       2016-07-11 13:58:51 +08:00
    kn007
        2
    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
        3
    kn007  
       2016-07-11 15:23:51 +08:00
    上面规则根据需要删减。 wp-data 和 wp-sign-in 是我构造的,评论不缓存,你也可以去掉。
    ivmm
        4
    ivmm  
    OP
       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
        5
    kn007  
       2016-07-11 15:29:44 +08:00
    @ivmm 打开隐身窗口,登录观察下 cookie ,看下 wordpress_logged_in_是否正常。实在不行,改成 pipe 看下。
    kn007
        6
    kn007  
       2016-07-11 15:30:09 +08:00
    另外后端是 8080 吧,你此贴写成 80 了
    ivmm
        7
    ivmm  
    OP
       2016-07-11 15:31:30 +08:00
    @kn007 现在改成了

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

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

    ivmm
        9
    ivmm  
    OP
       2016-07-11 15:37:42 +08:00
    @kn007 cookie 是这里的么
    kn007
        10
    kn007  
       2016-07-11 15:48:57 +08:00
    @ivmm pipe 行不行呢? pipe 不行的话,切回 nginx443 到博客目录试试
    ivmm
        11
    ivmm  
    OP
       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
        12
    kn007  
       2016-07-11 16:09:03 +08:00
    @ivmm Q 聊,还有那行 ban 是不是语法错了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2450 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:14 · PVG 10:14 · LAX 19:14 · JFK 22:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.