0

Is there a way for NGINX to have a different cache depending on the value of a cookie?

In my case, I have a website (in my case, a plugin) that creates a cookie called devicePixelRatio. This is 1 in most cases but if you have a high-DPI display then it will be equal to 2 (or more). Basically the server-side generates the website differently depending on the value of this cookie (either it is 1 or something else). Basically I would need two caches, one for devicePixelRatio=1 and one for any other value of devicePixelRatio.

Is that possible?

Thanks a lot.

TigrouMeow
  • 101
  • 1
  • 2

1 Answers1

1

Yes, that is possible.

https://stackoverflow.com/questions/26128412/how-to-extract-some-value-from-cookie-in-nginx

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

Use the client request headers to evaluate the cookie contents. Build your cache access logic around that.

More info: https://serversforhackers.com/nginx-caching/

Here is a starting point:

http://syshero.org/post/50053543196/disable-nginx-cache-based-on-cookies

You could, for example, place the proxy_cache_path in an if statement evaluating the cookie content. Pseudo code:

if $cookie_devicePixelRatio = 2
proxy_cache_path = /retina/;
(or fastcgi_cache_path)
else
proxy_cache_path = /sd/;
(or fastcgi_cache_path) 
JayMcTee
  • 3,923
  • 1
  • 13
  • 22