Nginx not returning 304 on cached content

cachenginxPROXY

I'm using nginx as a reverse proxy with an Apache back-end handling some PHP files.

The files return the right expiry headers and proxy_cache does a good job of caching them, but I've noticed that the cached content returns a 200 on every refresh, when it might be more efficient to return a 304 on the cached files.

The files in question are generated by PHP. The urls do not have .php in them as they've been prettified.

Any idea why nginx might not be returning 304 on repeated visits to a cached PHP output?

To clarify: It's using proxy_cache for caching dynamic PHP pages (not static html pages generated by PHP). I'm setting expires headers in the PHP file of time + 24 hours. With that in mind, I was hoping nginx would be able to then return 304s on its cached versions during that 24 hour window.

Best Answer

If php backend returns cookie header with request, the request is treated as new. Try adding this directive to nginx location with proxy_pass:

proxy_ignore_headers "Cache-Control" "Expires" "X-Accel-Expires" "Set-Cookie";

Related Topic