Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cookies Varnish:如何基于cookie更改站点的语言?_Cookies_Varnish_Varnish Vcl - Fatal编程技术网

Cookies Varnish:如何基于cookie更改站点的语言?

Cookies Varnish:如何基于cookie更改站点的语言?,cookies,varnish,varnish-vcl,Cookies,Varnish,Varnish Vcl,我有我的网站几乎工作。它只适用于一种语言,但我有一个设置语言的cookie。我也对它进行了哈希运算 问题是我不能改变我的饼干的价值,我不知道怎么做。 我的站点收到一个名为“lg=1”的变量,其中“1”是语言代码 我不知道如何将其传递到我的站点,以获取“英语”版本并再次保存新的cookie(使用lg=1值),因此,下次用户在没有lg=1变量的情况下访问时,他将基于cookie值访问我们的英语站点 有人能帮我吗? 谢谢如果您希望能够基于get参数设置cookie,您有两个选项 使用javascrip

我有我的网站几乎工作。它只适用于一种语言,但我有一个设置语言的cookie。我也对它进行了哈希运算

问题是我不能改变我的饼干的价值,我不知道怎么做。 我的站点收到一个名为“lg=1”的变量,其中“1”是语言代码

我不知道如何将其传递到我的站点,以获取“英语”版本并再次保存新的cookie(使用lg=1值),因此,下次用户在没有lg=1变量的情况下访问时,他将基于cookie值访问我们的英语站点

有人能帮我吗?
谢谢

如果您希望能够基于get参数设置cookie,您有两个选项

  • 使用javascript设置cookie
  • 将Varnish配置为始终将包含“lg=”的请求传递给您的应用程序,以便您可以在其中设置cookie

    sub vcl_recv {
        if (req.url ~ ".*lg=") {
            return (pass);
        }
    
        #Your other code in vcl_recv.....
    }
    

  • 您可能需要编辑VCL文件,默认情况下类似于/etc/varnish/default.VCL

    当您收到语言参数时,可以通过Varnish设置cookie。 然后使用cookie的值覆盖后端的请求url

    应该是这样的:

    sub vcl_recv {
      // Use the value of the cookie to override the request url.
      if (req.http.Cookie ~ "lg") {
        set req.url = req.url + "?" + req.http.Cookie;
      }
    
      // Go to VCL_error to redirect and remove the parametter.
      if (req.url ~ "(?i)lg=1") {
        error 802 "Remember to use the english version";
      }
    }
    
    
    sub vcl_error {
      /* Removes lg parameter and remember the english choice */
      if (obj.status == 802) {
        set obj.http.Set-Cookie = "lg=1; domain=." + req.http.host + "; path=/";
        set obj.http.Location = req.http.X-Forwarded-Proto + "://" + req.http.host + regsub(req.url, "\?lg=1", "");
        set obj.status = 302;
        return(deliver);
      }
    }
    

    我更愿意为每种语言使用不同的子域,比如当您收到一个参数时,您可以将用户重定向到子域。通过这样做,您不必每次都重写request.url。

    我将使用以下方法来实现:

  • 检查lgcookie是否存在。如果不存在,则使用后端服务器或varnish服务器进行设置
  • 我更喜欢使用varnish而不是后端服务器设置语言cookie,以避免后端服务器上的过度请求/负载
  • 默认语言选择必须为英语,即“1”
  • 根据用户语言选择设置哈希。这将有助于根据语言维护不同的缓存,并从缓存中检索数据

    请参阅以下代码:

    我使用了IPDcookie

    backend default {
        #applicable code goes here
    }
    sub identify_cookie{
        #Call cookie based detection method in vcl_recv
        if (req.http.cookie ~ "IPD=") {
                set req.http.Language = regsub(req.http.cookie, "(.*?)(IPD=)([^;]*)(.*)$", "\3");
        }
    }
    
    C{
        #used to set persistent(9+ years) cookie from varnish server.
        const char*  persistent_cookie(char *cookie_name){             
                time_t rawtime;
                struct tm *info;
                char c_time_string[80];
    
                rawtime = time(NULL) * 1.2; /*Added 9 years*/;
                info = localtime(&rawtime);
                strftime(c_time_string,80,"%a, %d-%b-%Y %H:%M:%S %Z", info);
    
                char * new_str ;
                if((new_str = malloc(strlen(cookie_name)+strlen(c_time_string)+1)) != NULL){
                        new_str[0] = '\0';   // ensures the memory is an empty string
                        strcat(new_str,cookie_name);
                        strcat(new_str,c_time_string);
    
                } else {
                        syslog(LOG_INFO,"Persistent cookie malloc failed!\n");
                }
                return new_str;
        }
    }C
    
    sub vcl_recv {
       call identify_cookie; #Used to get identify cookie and get its value
       if(!req.http.Cookie ~ "IPD"){
                C{
                        VRT_SetHdr(sp, HDR_REQ, "\006X-IPD:",persistent_cookie("IPD=1; domain=yourdomain.com; path=/; Expires="),vrt_magic_string_end);
                }C
       }
    }
    sub vcl_fetch {
       set beresp.http.Set-Cookie = req.http.X-IPD; #used for debug purpose only.Check cookie in response header
    }
    sub vcl_backend_response {
        #applicable code goes here
    }
    
    sub vcl_deliver {
        #applicable code goes here
        set resp.http.X-Cookie = "Cookies Set ("req.http.X-IPD")"; #used for debug purpose only.Check cookie in response header
    }
    sub vcl_error {
        #applicable code goes here
    }
    sub vcl_hash {
        hash_data(req.url);
        if (req.http.host) {
            hash_data(req.http.host);
        }
        if(!req.http.Language){
            req.http.Language = 1 #default english language
        }
        hash_data(req.http.Language); # make different hash for different language to avoid cache clashing
        return(hash);    
    }
    sub vcl_pipe {
        #applicable code goes here
    }
    sub vcl_pass {
        #applicable code goes here
    }
    
    注意:请确保仅更改cookie名称。正则表达式支持从多个cookie检索cookie值,而不考虑其顺序