perusio / drupal-with-nginx

Running Drupal using nginx: an idiosyncratically crafted bleeding edge configuration.
855 stars 246 forks source link

nginx cache MISS on unauthenticated users on drupal 6 (pressflow) but HIT on private window #286

Closed cptX closed 5 years ago

cptX commented 5 years ago

Hi to all, I'm struggling these days to implement Drupal 6 (pressflow) with nginx cache & purge capability (including cache expiration & purge modules). Everything looked normal and working until I noticed that when using browser's private window (so no cookie at all), cache produced HIT and when I was using normal browser as an unauthenticated user I was always getting MISS. $no_cache variable correctly detects if there is the SESS part in the cookie, so this is not the problem. I tried to check what is included in the request and response in the Private window compared to the normal window of the browser and I couldn't find the reason that normal window with unauthenticated user never gets HIT from nginx cache. So how is the mechanism of nginx cache deciding to fetch from the cache or not? $no_cache is 0 which should instruct the cache to provide the corresponding page...

As I'm in a dead-end here after a whole day of research, I'm kindly asking for your help.

UPDATE 1: The above are happening in my local setup. In my production server things are a bit different: The unauthenticated user always has a SESS in his cookie and this makes cache think it is a authenticated user ($no_cache = 1), which produces a BYPASS. In a private window there is no cookie and successfully I get a HIT.

So the question now becomes why Drupal 6 (Pressflow) has a SESS part in the cookie for the unauthenticated users?

You can check the cookie and headers here: http://crete.mmx.gr/lexiko and you can purge the same page like this: curl -X PURGE "http://crete.mmx.gr/purge/lexiko"

UPDATE 2: If I crear all cookies from my browser I always get a HIT in my first visit (as anonymous). All next visits are a MISS. So this means the existense of a cookie even without a SESS cancels nginx cache. This is very strange!

cptX commented 5 years ago

OK I finally solved it!!! The addition of the word "Vary" in the fastcgi_ignore_headers did the trick. The reason I suppose is that nginx internally checks this header and because in my site there was Vary: Cookie, nginx was avoiding saving this page in the cache... fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie" "Vary";

That was a hard one :)