varnish - 根据 cookie 中的值为同一页面提供不同的 Varnish 缓存?

标签 varnish

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

我正在建立一个支持多货币的商业网站,页面由 Varnish 提供。每次货币变化时,都会根据新的货币来改变cookie,而且由于请求头中的cookie发生了变化,会不会make varnish来创建不同的缓存?如果货币值(value)发生变化以显示正确的产品价格,我需要提供新内容。

如果varnish没有如上所述刷新,是否可以通过varnish配置来缓存cookie中不同货币值的不同内容或以某种方式修改页眉来实现?

最佳答案

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

最简单的方法是不缓存主页,并使用 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

(再次,让我们说名为“cry”的货币cookie)

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. apache2

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

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

关于varnish - 根据 cookie 中的值为同一页面提供不同的 Varnish 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558408/

相关文章:

android - 在 Android 中获取 503 作为 Http 响应?

php - Docker, Varnish ,对等连接重置

zend-framework - 使用Varnish/ESI和Zend框架的缓存和页面 View

nginx - Nginx产生的414错误变成了Varnish的503错误

php - Codeigniter : Varnish and Cookie/Session conflict

api - 缓存第三方API调用的最佳做法是什么?

linux - Varnish 错误 : vcl. 加载/etc/varnish/default.vcl 失败

caching - hash_data vcl_hash 的作用是什么?

caching - 如果存在 Varnish,为什么要使用 memcached

backend - Varnish似乎无法保持与后端的连接(错误503)