使用 Varnish 根据语言提供内容

使用 Varnish 根据语言提供内容

我的网站有多种语言。英语是默认语言,网址为:

http://domain.com/http://domain.com/en)。

当用户想要使用不同的语言时,他们会点击语言按钮。例如,他们想切换到日语:

http://domain.com/ja

那时,他们将拥有名为 SiteLang=ja 的 cookie。

我的 Varnish 配置:

backend default {
    .first_byte_timeout = 15s;
    .connect_timeout = 1s;
    .max_connections = 200;
    .between_bytes_timeout = 10s;
    .port = "8080";
    .host = "127.0.0.1";
}

sub vcl_recv {

    if (req.http.Cookie ~ "SiteLang=") {
            #unset all the cookie from request except language
            set req.http.Language = regsub(req.http.Cookie, "(?:^|;\s*)(?:SiteLang=(.*?))(?:;|$)", "\1.");
        } elseif (!req.http.Cookie) {
            set req.http.Language = "fr";
        }
    # Forward client's IP to backend
    remove req.http.X-Forwarded-For;
    set req.http.X-Forwarded-For = client.ip;

    # Set the URI of your system directory
    if (req.url ~ "^/system/" ||
        req.url ~ "ACT=" ||
        req.request == "POST" ||
        (req.url ~ "member_box" && req.http.Cookie ~ "exp_sessionid"))
    {
        return (pass);
    }

    unset req.http.Cookie;

    set req.grace = 1h;

    return(lookup);
}

sub vcl_fetch {

   #if (!beresp.http.set-Cookie ~ "SiteLang="){
   #     unset beresp.http.set-cookie;
   #}

    # Enable ESI includes
    set beresp.do_esi = true;

    # Our cache TTL
    set beresp.ttl = 1m;

    set beresp.grace = 1h;

    return(deliver);

}
sub vcl_deliver {

    if (req.http.X-Varnish-Accept-Language) {
        set resp.http.Set-Cookie = req.http.Language;
    }

}

sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    if (req.http.Language) {
        #add cookie in hash
        hash_data(req.http.Language);
    }
    return(hash);
}

当用户访问主页时,他们的首选语言已更改为英语。因此,Varnish 上的 Cookie 存在问题。

我想要的是当用户选择他们的语言时,他们无需更改语言即可进入主页,并且 varnish 可以正确提供缓存。

谢谢

答案1

我终于让它工作了。

backend default {
    .first_byte_timeout = 15s;
    .connect_timeout = 1s;
    .max_connections = 200;
    .between_bytes_timeout = 10s;
    .port = "8080";
    .host = "127.0.0.1";
}

sub vcl_recv {

    if (req.http.Cookie ~ "SiteLang=") {
            #unset all the cookie from request except language
            set req.http.Language = regsub(req.http.Cookie, "(?:^|;\s*)(?:SiteLang=(.*?))(?:;|$)", "\1.");
        } elseif (!req.http.Cookie) {
            set req.http.Language = "fr";
        }
    # Forward client's IP to backend
    remove req.http.X-Forwarded-For;
    set req.http.X-Forwarded-For = client.ip;

    # Set the URI of your system directory
    if (req.url ~ "^/system/" ||
        req.url ~ "ACT=" ||
        req.request == "POST" ||
        (req.url ~ "member_box" && req.http.Cookie ~ "exp_sessionid"))
    {
        return (pass);
    }

    #unset req.http.Cookie;

    set req.grace = 1h;

    return(lookup);
}

sub vcl_fetch {

   #if (!beresp.http.set-Cookie ~ "SiteLang="){
   #     unset beresp.http.set-cookie;
   #}

    # Enable ESI includes
    set beresp.do_esi = true;

    # Our cache TTL
    set beresp.ttl = 1m;

    set beresp.grace = 1h;

    return(deliver);

}
sub vcl_deliver {

    if (req.http.X-Varnish-Accept-Language) {
        set resp.http.Set-Cookie = req.http.Language;
    }

}

sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    if (req.http.Language) {
        #add cookie in hash
        hash_data(req.http.Language);
    }
    return(hash);
}

我的错误是:

#unset req.http.Cookie;

在子 vcl_recv 中将清空我的 cookie。

相关内容