簡體   English   中英

在重定向時覆蓋rails Cache-Control header

[英]Overriding rails Cache-Control header on redirect

我是否這樣做:

head 302

或者

head 307

或者

redirect_to

調用相同的 controller 動作

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

沒有效果。 Rails 發送:

Cache-Control: no-cache

無論。 我需要發送 Cache-Control header 來指示邊緣緩存為重定向服務一天。 這可能嗎?

您不能將 Cache-Control 直接設置到標頭中(不再?),因為您需要修改 response.cache_control object(因為稍后將使用它來設置 Cache-Control header)。

幸運的是, expires_in 方法會為您解決這個問題:

expires_in 1.day, :public => true

在此處查看更多信息: http://apidock.com/rails/ActionController/ConditionalGet/expires_in

使用 Rails 5,您可以做到

response.cache_control = 'public, max-age=86400'

嘗試改用這個

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

並確保您處於生產模式。 Rails 不會在開發中緩存。

. I need to send the Cache-Control header to instruct an edge cache to serve the redirect for a day.

這怎么可能? 在臨時重定向的情況下,瀏覽器將始終首先嘗試獲取原始 url,然后在重定向時他們將嘗試其他 url,如果緩存在代理上,則可以從那里提供服務。 但是再次瀏覽器仍然會與您的服務器進行首次聯系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM