是否可以使用相同的url和不同的cookie值(语言)来更改页面缓存(具有缓存版本)? [英] Is it possible to vary page caches (to have cache versions) with the same url and different cookie value (language)?

查看:145
本文介绍了是否可以使用相同的url和不同的cookie值(语言)来更改页面缓存(具有缓存版本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个多语言站点,并且想要缓存例如页面 /关于
英文页面和白俄罗斯语页面具有相同的URL: / about ,语言存储在cookie中并从cookie中检测出来。

We have a multilanguage site and want to cache e.g. page /about. English and belarusian pages have the same URL: /about, language is stored in and detected from the cookies.


  • En: / about ,cookie: site_lang:en

  • 要: / about ,cookie: site_lang:be

  • En: /about, cookie: site_lang: en
  • Be: /about, cookie: site_lang: be

我们有一个ReverseProxy缓存(SymfonyHttp或Varnish)。
当第一个用户打开Cookie中具有 en 值的 / about 页面时,它会由ReverseProxy和通过浏览器。
响应标头:

We have a ReverseProxy cache (SymfonyHttp or Varnish). When the first user opens the /about page with en value in cookies, it's cached by ReverseProxy and by browser. Response headers:

Cache-Control max-age=600, public, s-maxage=600
Last-Modified Mon, 27 Apr 2015 21:48:34 GMT
Vary X-Language
...



什么是 X语言



因为我们不能Cookies(至少由于会话ID而导致每个用户使用不同的缓存版本),我们会因Apache设置的自定义标头而有所不同。

What is X-Language:

Since we can't vary by Cookies (it will lead to have different cache versions for each user because of at least session id), we vary by custom header that is set by Apache.

因此,当用户发出GET请求后,Apache解析 site_lang cookie,并将解析后的值添加到自定义标头 X语言。有了这种逻辑,ReverseProxy知道该怎么做:如果此标头中没有某种语言的缓存,则只需返回缓存或将请求转发给应用程序即可。

So when user made a GET request, Apache parses site_lang cookie and adds parsed value to custom header X-Language. With this logic, ReverseProxy knows what to do: just return cache or forward request to application if there is no cache for some language from this header.

当第二个参数有效时用户打开 / about 页面,其中cookie中的 be 值,Apache解析此语言,创建 X语言:be 标头和ReverseProxy不会为<$​​ c $ c> en 语言返回缓存,而是将请求转发给应用程序。

It works when the second user opens the /about page with be value in cookies, Apache parses this language, creates X-Language: be header and ReverseProxy don't return cache for en language but forward request to application.

但是,当第一个用户尝试使用站点上的LocaleSwitcher更改语言环境时,它将无法正常工作。当尝试单击另一个语言链接时,浏览器立即返回本地缓存的页面,并且Apache不会处理用户的请求。

But it doesn't work when the first user tries to change locale with LocaleSwitcher on the site. When it tries to click to another language link, browser immediately returns local cached page and user's request is not processed by Apache, of course.

所以我的问题是,如何处理这种情况,可能的解决方案是什么。
主要要求是我们不想在URL中使用语言

So my question, how to handle this situation, what are the possible solutions. The main requirement is we don't want to have language in the URL.

谢谢。

推荐答案

您需要将 Vary:Cookie 发送给客户端,因为对于客户端而言,他得到的响应取决于他发送的cookie。但是,如果还有其他Cookie(例如Google Analytics(分析)),则将无法正常工作。

You would need to send a Vary: Cookie to the client because for the client, the response he gets depends on the cookie he sends. This will however not work if there are any other cookies like google analytics.

另一个明显的选择是在域或路径中使用语言环境,因此en.mydomain .com / about或mydomain.com/en/about。当人们将链接发送到页面时,因为语言是链接的一部分,所以这也更加友好。

The other obvious alternative is to use the locale in the domain or the path, so en.mydomain.com/about or mydomain.com/en/about. This is also more friendly when people send links to pages as the language is part of the link.

如果您的应用程序是用symfony构建的,则更改策略,并使用一种默认语言并且URL中没有语言环境的语言。

In case your application is built with symfony, its no problem to change the strategy, and also to have one language that is the "default" and does not have the locale in the URL.

这篇关于是否可以使用相同的url和不同的cookie值(语言)来更改页面缓存(具有缓存版本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆