max age with nginx/passenger/memcached/rails2.3.5

2019-04-13 14:04发布

问题:

I notice that in my production enviornment (where I have memcached implemented) in see a cache-control - max-age header in firebug, anytime I am looking at an index page (posts for example).

Cache-Control   max-age=315360000

In my dev environment that header looks like following.

Cache-Contro private, max-age=0, must-revalidate

As far as I know I have not done anything special with my nginx.conf file to specify max age for regular content, I do have expires-max set for css, jpg etc. here is my nginx.conf file..

http://pastie.org/1167080

So why is this cache-control is being set? How can I control this cache-control, because the side effect of this is kinda bad. This is what happens.

1 - User request all_posts listing and get a list of 10 pages (paginated)

2 - User view page 1, 2 3 and the respective caches are created.

3 - User goes back to page 1 and firefox does not even make a request to the server. Normally I would expect it would reqeust and hit the cache created in step #2.

The other issue is that if a new post has been created and now the cache is refreshed and it should be at the top of page 1, the user does not get to see it..because the browser isn't hitting the server.

Please help!

Thanks

Update :

I tried setting expires_now in my index action. NO difference the max-age is still the same large value.

Could this be a problem with my max-age regex? I basically want it to match only asset files(images, js, css etc)

回答1:

I think you're right that this is a problem with your max-age regex.

You're matching against this pattern: ^.+.(jpg|jpeg|gif|png|css|js|swf)?([0-9]+)?$

Because you have question marks ("this part is optional") after both parenthesized sections, the only mandatory part of the regex is that the request URI have at least two characters (.+.). In other words, it's matching pretty near every request to your site.

This is the pattern we use: \.(js|css|jpg|jpeg|gif|png|swf)$

That will match only requests for paths ending with a dot and then one of those seven patterns.