2015-07-21 91 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请求,并获取结果上
  • 通过您的中继服务器显示结果

可能会减慢整个事情 - 但我们在这里讨论肮脏的解决方法吗?希望这不是一个太疯狂的解决方案..