设置HAProxy以正确返回CORS标头的问题

时间:2018-10-13 18:53:36

标签: http cors haproxy

我需要有关CORS标头和HAProxy配置的帮助。

我在haproxy配置中添加了以下内容:

    # Add CORS headers when Origin header is present
capture request header origin len 128
http-response set-header Access-Control-Allow-Origin %[capture.req.hdr(0)] if { capture.req.hdr(0) -m found }
http-response set-header Access-Control-Allow-Methods GET,\ HEAD,\ OPTIONS,\ POST,\ PUT if { capture.req.hdr(0) -m found }
http-response set-header Access-Control-Allow-Credentials true if { capture.req.hdr(0) -m found }
http-response set-header Access-Control-Allow-Headers X-Stream-Output,\ X-Chunked-Output,\ X-Content-Length if { capture.req.hdr(0) -m found }
http-response set-header Access-Control-Expose-Headers X-Stream-Output,\ X-Chunked-Output,\ X-Content-Length if { capture.req.hdr(0) -m found }

似乎它向浏览器返回了CORS标头,但是在从浏览器进行的JSON调用中,我仍然收到403 Forbidden。 这是我要呼叫的网址:

https://ipfs.blockchaingraph.org:5001/api/v0/cat/QmdKPs1N6gH3R62BGssakAaEdFv14rp6fVVQy36yzdFQdT

当我尝试致电第三方类似查询时,它就可以正常工作:

https://ipfs.infura.io:5001/api/v0/cat/QmdKPs1N6gH3R62BGssakAaEdFv14rp6fVVQy36yzdFQdT

我比较了两个响应的标题。两者看起来相似。 我不知道我在这里想念什么。

我怀疑后端服务器正在基于某些标头阻止请求。我试图删除Origin:“ http-request del-header Origin”,但没有帮助

1 个答案:

答案 0 :(得分:1)

是的,这是后端服务器拒绝请求。 当我在haproxy配置中添加引用标头删除时,问题消失了:

http-request del-header Origin
http-request del-header Referer