本文介绍了Heroku& Rails-Varnish HTTP缓存不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的heroku网站的根页面本质上是静态的,它在生成时在视图中有一些红宝石代码,但是没有特定于单个用户的内容,因此我希望它由Varnish缓存并在不影响我的情况下提供dyno(请注意,应用程序中还有其他页面是动态的.)

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在此处看起来非常简单.只需添加response.headers['Cache-Control'] = 'public, max-age=300',它就会缓存5分钟,然后重新生成.

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.

为了测试这一点,我进行了更改并将日期(Time.now)输出到页面,以查看它是否在5分钟内保持不变,但是每次我在新的浏览器上访问该页面时,它都会更新.在同一浏览器上刷新可以正常工作,但是我认为这是由于浏览器缓存而不是Heroku造成的.

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.

如果有帮助,当我执行curl -i以获取HTTP标头时,会得到以下信息:

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

基本上,我想确保它一次生成然后在Varnish中缓存,我是否缺少任何选项,是否还需要配置其他内容?如果您有其他建议提供快速的静态页面,我也将非常喜欢.

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.

谢谢!

推荐答案

问题原来是Heroku拥有许多Varnish服务器,因此刷新页面大约需要10到15页,直到所有页面都被缓存为止.服务器,之后它将保持缓存状态.它一直都在工作,经过几次刷新后我还是放弃了.

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.

我还增加了max_age,以便可以更清楚地看到它,5分钟的缓存几乎没有效果.

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

这篇关于Heroku& Rails-Varnish HTTP缓存不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 23:18