【问题标题】:Using Varnish 3 to set cookies使用 Varnish 3 设置 cookie
【发布时间】:2014-12-31 15:42:55
【问题描述】:

我正在尝试使用 Varnish 设置 cookie,但没有看到如何处理多个同名标头。例如,如果需要设置多个 cookie,应用程序会发送:

Set-Cookie:sources=source+2; expires=Tue, 04-Nov-2014 17:12:50 GMT; path=/; domain=.xampp.com.local
Set-Cookie:someOtherCookie=othervalue; expires=Tue, 04-Nov-2014 17:12:50 GMT; path=/; domain=.xampp.com.local

在 Varnish 中,我只想设置一个 cookie,但如果后端响应包含 Set-Cookie 标头或客户端已经有 cookie,我想确保不会在设置我的 cookie 的过程。

来自 Nexcess 的 Turpentine 扩展似乎在 C 内部发挥了一些魔力,它负责设置会话:

sub generate_session {
    # generate a UUID and add `frontend=$UUID` to the Cookie header, or use SID
    # from SID URL param
    if (req.url ~ ".*[&?]SID=([^&]+).*") {
        set req.http.X-Varnish-Faked-Session = regsub(
            req.url, ".*[&?]SID=([^&]+).*", "frontend=\1");
    } else {
        C{
            char uuid_buf [50];
            generate_uuid(uuid_buf);
            VRT_SetHdr(sp, HDR_REQ,
                "\030X-Varnish-Faked-Session:",
                uuid_buf,
                vrt_magic_string_end
            );
        }C
    }
    if (req.http.Cookie) {
        # client sent us cookies, just not a frontend cookie. try not to blow
        # away the extra cookies
        std.collect(req.http.Cookie);
        set req.http.Cookie = req.http.X-Varnish-Faked-Session +
            "; " + req.http.Cookie;
    } else {
        set req.http.Cookie = req.http.X-Varnish-Faked-Session;
    }
}

sub generate_session_expires {
    # sets X-Varnish-Cookie-Expires to now + esi_private_ttl in format:
    #   Tue, 19-Feb-2013 00:14:27 GMT
    # this isn't threadsafe but it shouldn't matter in this case
    C{
        time_t now = time(NULL);
        struct tm now_tm = *gmtime(&now);
        now_tm.tm_sec += 3600;
        mktime(&now_tm);
        char date_buf [50];
        strftime(date_buf, sizeof(date_buf)-1, "%a, %d-%b-%Y %H:%M:%S %Z", &now_tm);
        VRT_SetHdr(sp, HDR_RESP,
            "\031X-Varnish-Cookie-Expires:",
            date_buf,
            vrt_magic_string_end
        );
    }C
}

他们正在检查用户是否已经拥有具有以下内容的前端 cookie:

if (req.http.Cookie !~ "frontend=") {   
    # it's a real user, make up a new session for them
    call generate_session;
}

那么,我该如何处理出现以下一种或多种情况的情况:

  • Varnish 正在为 Magento(松节油)添加 frontend= cookie
  • 后端返回一个或多个 cookie
  • 客户端已有一个或多个 cookie

【问题讨论】:

    标签: regex magento cookies varnish-vcl


    【解决方案1】:

    为此,您需要 header vmod:

    https://github.com/varnish/libvmod-header/

    例子:

    header.append(beresp.http.Set-Cookie,"stayAtLocale=1");
    

    您还可以使用std vmod 将所有标头“压缩”为一个:

    https://www.varnish-cache.org/docs/4.0/reference/vmod_std.generated.html#func-collect

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 1970-01-01
      • 2018-02-05
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      相关资源
      最近更新 更多