开发者

Varnish - cache when cookies / sessions are supplied in round robin director

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 nu开发者_开发技巧tshell:

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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜