Varnish - 当在循环控制器中提供 cookie/会话时进行缓存

发布于 2024-11-25 16:44:09 字数 373 浏览 2 评论 0原文

我是第一次配置清漆,我遇到了一个问题,到目前为止我还没有找到完整的答案。

我的配置简而言之:

server0 - varnish, be1-阿帕奇, be2 - apache

varnish 以循环方式从 2 个后端拉取。

我需要处理会话,因此我配置了一个简单的 php 脚本,该脚本在会话变量中设置当前时间,然后重定向到第二页,其中显示该会话变量和所使用的后端的 IP。

我已经成功地完成了这一半的工作。它正在传递我所看到的会话信息,但问题是 - 从后端返回的信息特定于生成它的后端。因此,返回的时间将取决于最后一次缓存调用的是 be1 还是 be2。

有没有一种方法可以让我正常工作,以便无论后端如何处理它,会话变量都是相同的?

I am configuring varnish for the first time, and I have come across an issue which I haven't been able to find a complete answer for so far.

My config in a nutshell:

server0 - varnish,
be1 - apache,
be2 - apache

varnish pulls from the 2 backends in a round robin.

I need to deal with sessions, so I have configured a simple php script which sets the current time in a session variable, then redirects to a 2nd page which displays that session variable and the ip of the backend that was used.

I have managed to get this half working. It is passing the session info from what i can see, but here is the problem - the info returned from the backend is specific to the backend it was generated on. So, the time returned will depend on whether be1 or be2 was called for the last cache.

Is there a way that I can get this working so that the session variable is the same regardless of the backend that processes it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2024-12-02 16:44:09

这是会话的普遍问题; cookie(随每个请求一起发送)包含会话的标识符。真实的会话数据存储在服务器或数据库中。有几种解决方案,例如:

  1. 粘性会话;设置额外的cookie变量(例如be=1或be=2)或使会话id可识别地属于后端1或2。然后Varnish可以决定请求应该发送到哪个后端服务器。如果后端出现故障,您的会话就会丢失(无“会话故障转移”)
  2. 将会话数据存储在数据库、memcached 或所有后端共享的其他数据存储中
  3. 不要使用会话,而是将所有数据序列化在 cookie 中。由于客户端不可信(cookie 可以由客户端更改),请确保验证数据是否未被篡改,例如对您的数据进行签名。

寻找一些可以申请您的代码的库/可重用代码,祝您好运!

This is the general problem with sessions; a cookie (sent with every request) contains an identifier for the session. The real session data is stored on a server or in a database. There are several solutions for this, e.g.:

  1. Sticky sessions; set an additional cookie variable (e.g. be=1 or be=2) or make the session id recognizably belong to backend 1 or 2. Varnish can then decide to which backend server the request should go. If a backend goes down, your session is lost (no "session fail over")
  2. Store the session data in a database, memcached, or some other datastore shared by all backends
  3. Don't use sessions but serialize all your data in a cookie. As clients are untrustable (cookies can be changed by a client) make sure to verify if the data is untampered, e.g. sign your data

Look for some library/reusable code you can apply for your code and good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文