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

发布于 2024-10-03 00:55:56 字数 397 浏览 1 评论 0原文

我有一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-10-10 00:55:56

通常动态生成的页面没有缓存,因此

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

标头是正确的起点。

尝试使用“使用基于 Web 的服务”以查看它们是否出现在从您的站点发回的 HTTPd 标头中。

Usually dynamically generated pages have no caching so the

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

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.

帥小哥 2024-10-10 00:55:56

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

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

You can also access the header fields of the response object with this syntax:

response['Cache-Control'] = 'public, max-age=600'
简美 2024-10-10 00:55:56

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

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

  # Your magic goes here
end

In Sinatra you can use the cache_control method:

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

  # Your magic goes here
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文