2015-07-21 92 views
3

我使用碼頭清漆 - 請參閱million12/varnish如何緩存與清漆的請求?

GET請求很好用!

但我不知道我必須設置緩存POST請求的設置。

在谷歌我發現很多帖子(從2010年或2011年),它說POST請求不能緩存與清漆 - 這種說法仍然正確?

還有另一種緩存POST請求的方法嗎?

這裏我varnish.vcl設置:

vcl 4.0; 
backend default { 
    ... 
} 

# Respond to incoming requests. 
sub vcl_recv { 
    unset req.http.Cookie; 
} 

# Set a header to track a cache HIT/MISS. 
sub vcl_deliver { 
    if (obj.hits > 0) { 
    set resp.http.X-Varnish-Cache = "HIT"; 
    } 
    else { 
    set resp.http.X-Varnish-Cache = "MISS"; 
    } 
} 

# Not cache 400 - 500 status requests 
sub vcl_backend_response { 
    if (beresp.status >= 400 && beresp.status <= 600) { 
     set beresp.ttl = 0s; 
    } 
} 

感謝您的幫助!

+0

爲什麼要緩存Post請求?我認爲在概念上是錯誤的。看看[this](http:// stackoverflow。com/questions/626057/is-it-it-it-cache-post-methods-in-http) – Redithion

+0

我用公司內部的API計算大事情。 有幾乎相同的POST請求。 這就是爲什麼我正在尋找一個解決方案來緩存POST請求以獲得更好的性能 – user1199255

+0

如果你在後端檢查它會不會更好? – Redithion

回答

5

當前清漆無法緩存POST請求

AFAIK人試圖緩存POST請求失敗。看起來Varnish最終將這些轉換爲GET請求。

來源:

+0

我認爲這已不再是實際的。 –

+0

@VladislavRastrusny你能提供關於這方面的更多信息嗎? – Redithion

+0

我的意思是@Laizer的答案。 –

5

有清漆模塊和教程緩存POST請求。這增加了將帖子主體添加到散列鍵並傳遞POST請求的功能。

的VMOD可用於清漆4版本,幷包括以下 功能:

buffer_req_body(BYTES size): buffers the request body if it is smaller 
    than size. This function is a 「better」 (bug-free**) copy of 
    std.CacheReqBody(), so please use this one instead of the one provided 
    by the libvmod-std. Please note that retrieving req.body makes it 
    possible to retry pass operations(POST, PUT). 

len_req_body(): returns the request body length. 

rematch_req_body(STRING re): regular expression match on request body. 

hash_req_body(): adds body bytes to the input hash key. 

https://info.varnish-software.com/blog/caching-post-requests-with-varnish https://github.com/aondio/libvmod-bodyaccess

注意,在這個VMOD的4.1分支,內置的std.cache_req_body()被用來代替buffer_req_body(),但是Varnish 4.1中的一個常見錯誤會破壞4.1分支。 https://github.com/varnishcache/varnish-cache/issues/1927

0
  • 把另一ngnx /阿帕奇/任何未使用的端口
  • 推POST請求到服務器
  • 在那裏你轉發給清漆作爲GET請求,並獲取結果上
  • 通過您的中繼服務器顯示結果

可能會減慢整個事情 - 但我們在這裏討論骯髒的解決方法嗎?希望這不是一個太瘋狂的解決方案..