简体   繁体   中英

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 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?

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, eg:

  1. Sticky sessions; set an additional cookie variable (eg 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, eg sign your data

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM