5

I'm trying to set up Nginx as a reverse proxy for my Rails application running on Unicorn.

My app has some public pages that I'd like to cache for a duration for 60 minutes, so I set the max-age header.

Furthermore I have added the proxy_cache option to my server block in Nginx.

proxy_cache default; 

and defined this cache

proxy_cache_path /var/www/nginx_cache keys_zone=default:10m max_size=500m; 

I have set up my application to add this header to my cachable responses

Cache-Control: max-age=3600, public 

When a client hits the page on the first time, the page is rendered and a response is returned. However, on subsequently requests, nginx does not decrement the max-age counter.

As a result, a visitor may visit my page one minute before it expires, meaning he is seeing 59 minutes old content. But then keep this copy in his browser for another 60 minutes, before he'll get a new copy.

Can I get Nginx to subtract the time passed by, so if a visitor hits a page that has been in the Nginx cache for 25 minutes, the max-age will be 35 minutes (2100 seconds) ?

4
  • If the client revalidates and the page hasn't changed, then what's the problem? Commented Jan 16, 2015 at 20:32
  • There is no validation in the equation and there won't be. I think the problem is pretty clear. I tell Nginx the response is good for an hour, so when a client requests the resource 50 minutes later, it's only good for another 10 minutes, not another 60 minutes. Commented Jan 16, 2015 at 22:00
  • @Niels Hi did you find a solution for this? Commented Dec 20, 2019 at 23:22
  • @Vixxs This question is 5 years old, so I don't remember clearly. I don't think I got the decrement strategy to work. Commented Dec 21, 2019 at 7:33

1 Answer 1

1

You have completely wrong approach.

Please do read more about Nginx proxy caching http://nginx.org/en/docs/http/ngx_http_proxy_module.html

This can also be usefull: How to set up Nginx as a caching reverse proxy?

2
  • 1
    Can you elaborate on what is wrong about my approach rather than linking to a huge document? Commented Jan 19, 2015 at 16:48
  • I think is a valid question, some caches send Age header like Varnish, decrement the max-age can work too. Commented Dec 20, 2019 at 23:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.