【问题标题】:Serve different varnish cache for the same page based on a value in cookie?根据cookie中的值为同一页面提供不同的清漆缓存?
【发布时间】:2014-05-22 01:30:52
【问题描述】:

这个问题可能根本没有意义,因为我是清漆新手,但这里是:

我正在构建一个支持多币种的商业网站,并且页面由清漆提供。每次币种变化时,都会根据新币种改变cookie,并且由于请求头中的cookie发生变化,是否会做varnish创建不同的缓存?如果货币价值发生变化以显示正确的产品价格,我需要提供新内容。

如果varnish没有像上面提到的那样刷新,是否可以通过varnish配置缓存cookie中不同货币值的不同内容或以某种方式修改页面标题来实现?

【问题讨论】:

    标签: varnish


    【解决方案1】:

    首先,很抱歉(非常)迟到的答案。希望对你有帮助!

    最简单的方法是不缓存主页,并使用 PHP 或 apache 或 nginx 的 mod_rewrite 从主页重定向。

    第 1 步 - 不缓存主页

    清漆

    default.vcl

    sub vcl_recv {
        ...
    
        # dont cache the homepage
        if (req.url ~ "^/") {
            return(pass);
        }
    
        ...
    }
    

    记得在任何更改后重新启动 varnish。

    第 2 步 - 重定向到货币页面

    在此处,选择以下选项之一进行重定向。

    1。 PHP

    (假设货币 cookie 名为“cry”)

    index.php

    if (!empty($_COOKIE["cry"]))
    {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://www.yourdomain.co.uk/?currency=".$_COOKIE["cry"]); 
    }
    

    2。 nginx

    (再次,假设货币 cookie 名为“cry”)

    sites-enabled 中的

    yoursite 文件 - nginx

    server {
        listen       80;
        server_name  bestsellers.co.uk www.bestsellers.co.uk;
    
        location = / {
            if ($cookie_cry ~* "usdollars") {
                rewrite ^ http://www.yourdomain.co.uk/?currency=usd permanent;
            }
            if ($cookie_cry ~* "ilshekels") {
                rewrite ^ http://www.yourdomain.co.uk/?currency=ils permanent;
            }
            if ($cookie_cry ~* "rusruble")  {
    
            ... and more ...
        }
    
        ...
    }
    

    记得在任何改动后重启 nginx。

    3。阿帕奇2

    我真的不知道如何在 apache 中做到这一点,但 google 可以帮助你。'


    你应该没事的。 http://www.yourdomain.com/?currency=usd 将不同于 http://www.yourdomain.com/?currency=ils。如果您有任何问题,请在此处发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多