4

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.

2
  • lets clarify this: nginx serves static .html files generated by php or it caches content it recieves from apache trough proxy_pass directive? Oct 31, 2012 at 19:14
  • I've clarified the question. Thanks for pointing that out! Hopefully its clearer now.
    – Don H
    Nov 2, 2012 at 11:07

3 Answers 3

1

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";

1

Having poked around at various things, it turns out the answer was that I'd neglected to declare a "Last modified" header in the php. Since the page changes on a daily basis I set the last modified date to the previous midnight. It is now returning 304s as expected.

1
  • You should mark your answer as the correct answer for others to identify it faster (if it solved your problem, which it seems it did).
    – Oliver
    Apr 25, 2013 at 20:48
0

Because PHP is generating the HTML files on the fly, they are always new (as far as Nginx is concerned).

If you want to cache PHP - you need to use an op-code cache like APC or xCache.

1
  • I'm hoping to avoid caching within PHP. The intent was to have proxy_cache in Nginx serve the static pages without spinning up another Apache and PHP process.
    – Don H
    Nov 2, 2012 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .