Лак: как отдельно кешировать страницы на основе значения определенного файла cookie

Я управляю сайтом с одним файлом cookie, который мы должны использовать, но всегда будет одним из 9 значений (в том числе без значения). Я бы хотел использовать лак перед нашими серверами приложений, при этом лак отдельно кэшировал версию каждой страницы на основе значения cookie.

Поэтому, если у нас есть страница / страница1, Varnish должен отдельно управлять копией того, что / page1 выглядит с помощью значений cookie a, b, c, d и т. Д ….

Предположим, у нас на сервере Varnish достаточно памяти, чтобы обрабатывать все страницы со всеми комбинациями файлов cookie.

Мы пробовали множество настроек VCL, но не можем точно определить, как это сделать. Лак должен отправить этот конкретный файл cookie на наш сервер приложений, так что наше приложение знает, какой контент отправить.

Заранее спасибо!

Solutions Collecting From Web of "Лак: как отдельно кешировать страницы на основе значения определенного файла cookie"

Достаточно просто добиться того, что на самом деле вы должны добавить пользовательский vcl_hash :

 sub vcl_hash { #... /* Hash cookie data */ # As requests with same URL and host can produce diferent results when issued with different cookies, # we need to store items hashed with the associated cookies. Note that cookies are already sanitized when we reach this point. if (req.http.Cookie) { /* Include cookie in cache hash */ hash_data(req.http.Cookie); } #... } 

С помощью этого кода лак будет хранить другой файл cookie для каждого значения cookie … но я также рекомендую вам также дезинфицировать ваши файлы cookie на vcl_recv , это выдержка из [1], содержащая санировку cookie для сайтов Drupal:

 sub vcl_recv { #... # Remove all cookies that backend doesn't need to know about. # See https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies if (req.http.Cookie) { /* Warning: Not a pretty solution */ /* Prefix header containing cookies with ';' */ set req.http.Cookie = ";" + req.http.Cookie; /* Remove any spaces after ';' in header containing cookies */ set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); /* Prefix cookies we want to preserve with one space */ /* 'S{1,2}ESS[a-z0-9]+' is the regular expression matching a Drupal session cookie ({1,2} added for HTTPS support) */ /* 'NO_CACHE' is usually set after a POST request to make sure issuing user see the results of his post */ /* Keep in mind we should add here any cookie that should reach the backend such as splahs avoiding cookies */ set req.http.Cookie = regsuball(req.http.Cookie, ";(S{1,2}ESS[a-z0-9]+|NO_CACHE|OATMEAL|CHOCOLATECHIP)=", "; \1="); /* Remove from the header any single Cookie not prefixed with a space until next ';' separator */ set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); /* Remove any '; ' at the start or the end of the header */ set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); if (req.http.Cookie == "") { /* If there are no remaining cookies, remove the cookie header. */ unset req.http.Cookie; } } #... } 

[1] https://github.com/NITEMAN/varnish-bites/blob/master/varnish3/drupal-base.vcl