Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

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

3 Answers

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

share|improve this answer
add comment
up vote 1 down vote accepted

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.

share|improve this answer
    
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 '13 at 20:48
    
Thanks, Oliver. –  Don H Apr 26 '13 at 9:59
add comment

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.

share|improve this answer
    
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 '12 at 11:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.