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) ?

Niels B.
  • 363
  • 1
  • 3
  • 10
  • If the client revalidates and the page hasn't changed, then what's the problem? – Michael Hampton Jan 16 '15 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. – Niels B. Jan 16 '15 at 22:00
  • @Niels Hi did you find a solution for this? – Vixxs Dec 20 '19 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. – Niels B. Dec 21 '19 at 07:33

1 Answers1

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?

Nemke
  • 248
  • 5
  • 9
  • 1
    Can you elaborate on what is wrong about my approach rather than linking to a huge document? – Niels B. Jan 19 '15 at 16:48
  • I think is a valid question, some caches send Age header like Varnish, decrement the max-age can work too. – Vixxs Dec 20 '19 at 23:24