Caching Plone site using cookie negotiation with Varnish

Something I learned only today that felt neat enough to share.

Problem:

How to cache a multilingual Plone site that uses cookie negotiation for language selection with Varnish.

Solution:

  1. Keep dropping cookies at Varnish.

  2. Enable also request based language negotiation (Accept-Language).

  3. Configure Plone caching operations to set Vary: Accept-Language.

  4. Configure Varnish to map I18N_LANGUAGE-cookie value to Accept-Language-header.

    if (req.http.cookie ~ {"I18N_LANGUAGE="[^"]+""}) {
        set req.http.Accept-Language = regsub(req.http.cookie, {".*I18N_LANGUAGE="([^"]+)".*"}, "\1");
    } else {
        unset req.http.Accept-Language;
    }
3 Likes