如何在 Heroku 上托管的 Ruby/Sinatra 应用程序中设置 HTTP 标头?
我有一个基于 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
I've got a working app based in Ruby and Sinatra that is deployed on Heroku.
I want to take advantage of the HTTP caching available on Heroku, which uses Varnish.
I'm not sure what the best way to set the headers is, and the correct syntax.
Any thoughts on the best approach and syntax?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常动态生成的页面没有缓存,因此
标头是正确的起点。
尝试使用“使用基于 Web 的服务”以查看它们是否出现在从您的站点发回的 HTTPd 标头中。
Usually dynamically generated pages have no caching so the
header is the right starting point.
Try using one of the services at "Use a Web-based service" to see if they show up in the HTTPd header sent back from your site.
您还可以使用以下语法访问响应对象的标头字段:
You can also access the header fields of the response object with this syntax:
在 Sinatra 中,您可以使用
cache_control
方法:In Sinatra you can use the
cache_control
method: