Viktor Bijlenga

Viktor Bijlenga

  • Blog
  • About
  • NGINX cache based on cookies

    I’ve been trying to let the NGINX fast-CGI cache serve different versions of a site, depending on a cookie value.

    I want to use the cache, in this case, not skip it. If you want to skip the cache by cookie, read the excellent guide on caching by Deliocious Brains in their Ubuntu server guide.

    To be able to serve different pages, I needed to update thefastcgi_cache_key directive. The directive is located in the /etc/nginx/sites-available/SITENAME/server folder, in a file named FastCGI-cache.conf when using SpinupWP.

    You need to update it like this, I’ve keept the original line as a comment.

    # The key to use when saving cache files, which will run through the MD5 hashing algorithm.
    # fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_YOUR_COOKIE_NAME";

    You can access cookie values in NGINX by using the $cookie_YOUR_COOKIE_NAME variable – a list of available NGINX variables can be found in the Nginx documentation.

    This results in an NGINX cache which is different depending on the cookie value. This can be used to serve a dark theme and a light theme according to users’ preferences in a cookie, or different content security policies.

    NOTE: This might create a lot of cached pages, which might result in a large number of files. Use with a bit of caution on larger sites.

    NOTE 2: if you spot any errors, or have suggestions on how to improve further, please let me know.

    Feb 14, 2022
  • November 20, 2018 at 09:47PM

    45672806_1147900915385486_1109826492556258620_n

    Ägnade alldeles för mycket tid att skriva fina ord som bildtext, men inget känns tillräckligt bra. Så jag sparar bilden här istället. Så jag kan minnas hur mycket fin energi en stund med dessa två alltid ger. Love you!

    Nov 20, 2018
  • November 19, 2018 at 11:53AM

    43611554_366488824101640_5950295512747711393_n

    I verkligheten var det blåsigt och väldigt trevligt.

    Nov 19, 2018
  • October 29, 2018 at 08:40AM

    43750293_1934503110179758_6472752495921417923_n

    Inser att 97% av mitt flöde är motljus, så här kommer en till. Från häromdagen när jag gick mot bussen.

    Oct 29, 2018
  • October 18, 2018 at 08:04AM

    42429218_179229206319485_6566880238692174282_n

    Oct 18, 2018
  • May 25, 2018 at 07:35PM

    31977740_2289831641244215_6806637821076439040_n

    May 25, 2018
  • April 28, 2018 at 09:43PM

    30855563_225982604802656_6391634694839992320_n

    Apr 28, 2018
  • February 24, 2018 at 06:50PM

    27890831_343100822870567_8634132962276802560_n

    Vintersol på favvoställe

    Feb 24, 2018
  • January 30, 2018 at 08:59AM

    26283563_330436777451337_8505104396583960576_n

    Vinterkänsla idag på Länsmansgatan 👌

    Jan 30, 2018
  • December 17, 2017 at 11:09PM

    25009692_1496563003984161_3196467906116321280_n

    Vinterljus

    Dec 17, 2017
  • December 17, 2017 at 11:08PM

    25007647_740125869526371_2623242947813965824_n

    Hemhem

    Dec 17, 2017
  • December 13, 2017 at 08:50PM

    24845178_906599776156999_1176701492984283136_n

    En av mina bästa hjältar: Pappa Bosse. Du är världens bästa.

    Dec 13, 2017
  • November 04, 2017 at 12:43PM

    23101640_1933283530254741_8302397543828946944_n

    För ett par veckor sen var jag i London. Himla fint att tänka tillbaka på. Glad helg på dig!

    Nov 4, 2017
  • Fyller på höstenergi

    22857964_1504072836373157_8821818987252809728_n
    via Instagram https://www.instagram.com/p/Bayfi8cA6TZ/

    Oct 28, 2017
  • 26 oktober

    22793695_283125608856714_5360353922550595584_n
    via Instagram https://www.instagram.com/p/Bat6j-7AI62/

    Oct 26, 2017
1 2 3 … 24
→
  • Blog
  • About