如何在Heroku上托管的Ruby / Sinatra应用程序中设置HTTP标头?

时间:2010-11-16 22:18:58

标签: ruby http-headers heroku sinatra varnish

我有一个基于Ruby和Sinatra的工作应用程序部署在Heroku上。

我想利用Heroku上可用的HTTP缓存,它使用Varnish。

我不确定设置标头的最佳方法是什么,以及正确的语法。

关于最佳方法和语法的任何想法?

before do
    headers "Content-Type" => "text/html; charset=utf8"
end

get '/' do
    headers['Cache-Control'] = 'public, max-age=600'

    # SOME STUFF HERE

    haml :home, {:layout => :layout_minfooter}

end

3 个答案:

答案 0 :(得分:30)

通常动态生成的页面没有缓存所以

response.headers['Cache-Control'] = 'public, max-age=300'

标题是正确的起点。

尝试使用“Use a Web-based service”中的某个服务,看看它们是否显示在从您的网站发回的HTTPd标头中。

答案 1 :(得分:0)

您还可以使用以下语法访问响应对象的标头字段:

response['Cache-Control'] = 'public, max-age=600'

答案 2 :(得分:0)

在Sinatra中,您可以使用cache_control方法:

get '/' do
  # Cache for 24 hours
  cache_control :public, max_age: 86400

  # Your magic goes here
end
相关问题