perusio / drupal-with-nginx

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

Varnish won't work because cache-control is being sent as nocache #275

Open MaffooBristol opened 7 years ago

MaffooBristol commented 7 years ago

I may be missing something, but the config seems to force main pages on Drupal to send a Cache-Control: nocache header, which stops Varnish from doing its thang. I am aware that microcaching is used, but should it not also respect the drupal cache settings?

Thanks

guiajlopes commented 7 years ago

where are you seeing the nocache header? I use this setup with varnish and it works really well

MaffooBristol commented 7 years ago

image

guiajlopes commented 7 years ago

I mean where are you seeing the header no-cache being set on nginx config?

MaffooBristol commented 7 years ago

I'm not sure, I'm using this config mostly out of the box, but I have no idea what the cause/origin is!

guiajlopes commented 7 years ago

Probably not, I search on the repo for no-cache and haven't found. I saw that you have cookie on your request, is it the session? If so keep in mind that the drupal page cache doesn't work for logged in users and drupal always send no-cache in this case. If you are testing as anonymous user and still having the problem with no-cache there it's being set in another place.

MaffooBristol commented 7 years ago

Hmm, it is odd. And no, it's the same for anonymous users. I guess it's somewhere else but I'm not sure where, perhaps it's just my Varnish config that's screwed, even though I used (again) an out-of-the box example from, I think d.org

guiajlopes commented 7 years ago

The no-cache for anonymous make totally sense since the website have different behaviours per user.

Probably it's something on your varnish config, or also in the drupal cache configuration. Take a look into this varnish vcl file, it should work fine. https://github.com/Lullabot/varnish

MaffooBristol commented 6 years ago

Thanks. I actually turned Varnish off completely and am still getting no-cache coming up. I also have this constant bug with microcaching being used when logged in, and on admin pages, so that the admin pages get stuck in a loop (for example, running cron does nothing first time, takes about 5 attempts... or saving a page will show the drupal_set_message for seconds before it disappears again)

MaffooBristol commented 6 years ago

I just created a completely blank, vanilla D7 site. Turned off all Varnish and Redis and the like, and the headers still come back with cache-control: no-cache. I have no more ideas!

$ curl -I mysite

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 30 Jul 2017 16:22:03 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Keep-Alive: timeout=10
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Etag: "1501431723-0"
Content-Language: en
X-Frame-Options: SAMEORIGIN
Cache-Control: no-cache
Last-Modified: Sun, 30 Jul 2017 16:22:03 GMT
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Vary: Cookie
Vary: Accept-Encoding
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Content-Options: nosniff
MaffooBristol commented 6 years ago

Okay, small update, I found what was doing it.

If you look in /etc/nginx/apps/drupal/microcache_fcgi.conf, there are these lines:

## Bypass the cache.
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;

## To avoid any interaction with the cache control headers we expire
## everything on this location immediately.
expires epoch;

... so as stated, it seems to be using microcaching. But why? It completely overwrites the caching settings in Drupal, which I think is really bad. Especially if it's not made obvious where this is coming from or how it's working! Uncommenting those lines has made http://www.isvarnishworking.com/ go from "sort of" to "yes", due to now having an age > 0

So, this thread is maybe more of a discussion? Is this the right behaviour?

MaffooBristol commented 6 years ago

There's also still the weird bug with caching on admin pages. Now, the headers coming from Drupal are working perfectly Age:0 and Cache-Control:no-cache, must-revalidate, however I'm getting X-Micro-Cache:HIT. This microcache should definitely not be working on admin pages, so I'm assuming I'll have to entirely comment that out too

guiajlopes commented 6 years ago

doesn't make sense have Microcaching if you have a varnish in front of it. I think the config of microcaching make sense only when you don't have an http cache layer...

In my infra I don't use microcaching, just a reverse proxy and drupal cache