I have a cache invalidation system in place, so I need to set the cache for css|js to never expire.

I tried this

location ~* \.(js|css)$ { # |png|jpg|jpeg|gif|ico
  expires max;
  #log_not_found off; # what's this for?

And this is what I see in firebug:

enter image description here

So as you can see, requests are still being made, though they only receive a 'not modified' response. But I want to avoid them entirely, is that possible?

Also, I've read that an expirires of more than 1 year is or will be considered invalid in the standard. Is that true?


Let me complete @DisgruntledGoat's answer:

  • Normal access to the url, like clicking on a link or in the url bar + enter: cache works, only 1 request issued for that url, and 0 for the assets.
  • F5 to reload: many requests issued, but you receive all '304 not modified' responses for the assets, so they are not downloaded
  • ctrl + F5 to reload without cache: many requests, all '200 success', all the assets are downloaded again.
  • 1
    I've suggested this topic be migrated to Server Fault where their users have much more experience running servers including Nginx, you'll get better help there. – Anagio Aug 19 '12 at 5:08

Are you reloading the page to test this? When you do that, browsers generally request every file again (at least Chrome does). If you instead click links to different pages on your site, the browser should just use the cache and not re-request files.

Additionally, according to the spec the Expires directive can only have a date up one year in the future, so 2037 is an invalid value. Having said that, browsers generally accept far-future values just fine.

  • You are right, click on the url + enter (to reload without using f5) does not request every file again. Thanks! – ChocoDeveloper Aug 20 '12 at 4:43

This cannot be done in another way you try it. It depends on the clients browser how cache is handled. Setting cache should not exceed one year more than the modification date Source.

Nginx is very effective, if you have < 1,000,000 visitors every day, you really don't have to worry about requests sent to Nginx. If it is about bandwidth, if a browser gets a 304 responce, it still uses the data from its cache, so no bandwidth is spilled.

  • From that link: once they're set and the resource is downloaded, the browser will not issue any GET requests for the resource until the expiry date or maximum age is reached. But my browser is issuing GET requests, for some reason. Though as you said, it's not using badwidth because of the 304 response. – ChocoDeveloper Aug 18 '12 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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