$sent_http_content_language Doesn't working

By | December 7, 2017

My django site have 2 languages and when the language change, nginx hits the cache with old language and language doesn’t change (Of course, not cached pages changing). To prevent that I need to add “$sent_http_content_language” variable to my “proxy_cache_key” but not working, even if I tried with :

    add_header Test $sent_http_content_language;


My Response Headers


I’m gonna take a stab… $sent_http_HEADER doesn’t make a whole lot of sense for a cache key, since that’s part of the response. nginx doesn’t have access to the response until it actually contacts the upstream server and performs the request.

note that there’s no way for nginx to tell the upstream server “Hey… based on the response headers, you’ve just sent, i’ll serve the rest of the request from cache”.

So… if you can’t tell from the request that the response is already in cache, you cannot use the cache at all.

One way to resolve this is to use a cookie; you can add the users’ language preference to a cookie, which will be returned by the client on subsequent requests. Since that value is present on the request, you can use it in a cache key (like $cookie_lang, if the response had a Set-Cookie: lang=tr).

There’s a related problem to this; even if you had this sort of caching at the server, using the response’s Language header, working; you would still not see an update in the client; because it would be in the browser cache. You cannot turn that cache off, and you cannot control it, except with the handful of cache-control headers. From the content you’ve posted, it looks like they are already set in a sensible way (5 minutes expired, Vary header lists cookie). That means that it’s up to you to honor that promise, and only change the response content in agreement with those request headers.

Fortunately, adding a cookie to express the language does honor the cache-control headers you’re using.

Leave a Reply

Your email address will not be published. Required fields are marked *