Heroku & Rails - Varnish HTTP Cache Not Working

你离开我真会死。 提交于 2020-01-05 04:03:06

问题


My heroku website's root page is essentially static, it has some ruby code in the view when its generated, but there's nothing specific to a single user, so I'd like to have it cached by Varnish and served up without hitting my dyno (note that there are other pages that are dynamic in the application).

Heroku makes it seem very simple here. Just add response.headers['Cache-Control'] = 'public, max-age=300' and it'll cache for 5 minutes before regenerating.

To test this I made the changed and outputted the date (Time.now) to the page, to see if it would remain the same for 5 minutes, but every time I access the page on a new browser it updates. Refreshing on the same browser works fine, but I think that's because of the browser caching, not Heroku.

If it's any help, when I do a curl -i to get the HTTP headers, I get this:

HTTP/1.1 200 OK^M
Server: nginx/0.7.67^M
Date: Thu, 29 Dec 2011 02:03:33 GMT^M
Content-Type: text/html; charset=utf-8^M
Connection: keep-alive^M
Cache-Control: public, max-age=300^M
X-Ua-Compatible: IE=Edge^M
Etag: "8a1b58f048968803980417a2914764d0"^M
X-Runtime: 0.038393^M
Content-Length: 8310^M
X-Varnish: 1040651825^M
Age: 0^M
Via: 1.1 varnish^M

Basically, I'd like to make sure that it's generated once and then cached in Varnish, am I missing any options, do I have to configure something more? If you have other suggestions for serving a fast static page I'd love those as well.

Thanks!


回答1:


I beleive that there is something in your application causing this issue (possibly the headers aren't being set correctly?).

To test this I put up an application that contains one cached, and one un-cached page (nothing other than rendering the text to the output.

http://morning-earth-1972.heroku.com/base/cache

http://morning-earth-1972.heroku.com/base/no_cache

Notice that the cache version will not change within five minutes of the time printed.

The source can be found here: https://github.com/neilmiddleton/heroku_varnish_test_SO_8663326

Therefore, Heroku is working as you would expect. I would initially look at your auth system to check that that isn't doing something, as this is quite a common scenario (to ensure that cached pages are fully secured)




回答2:


Maybe you are sending cookies when testing with your browser ? What are your request headers ?

What do you get if your run curl -I http://host.com/ twice ?




回答3:


The problem turned out to be that Heroku has a number of Varnish servers, so it takes about 10-15 page refreshes until the page was cached on all of the servers, after that it would stay cached. It was working all along, I just gave up after a few refreshes didn't work.

I also increased the max_age so that I could see it more clearly, a 5 minute cache has almost no effect.



来源:https://stackoverflow.com/questions/8663326/heroku-rails-varnish-http-cache-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!