Curl到Google Compute负载平衡器会收到错误502 [英] Curl to Google Compute load balancer gets error 502

查看:608
本文介绍了Curl到Google Compute负载平衡器会收到错误502的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我卷曲一个POST请求与文件上传到我的谷歌计算负载均衡器(LB),我得到一个502错误。如果我对LB后面的工作节点执行相同的curl,则工作。如果我使用像PHP Guzzle这样的库,它的工作方式。如果我在LB上做一个基本的GET请求,我得到正确的响应,但工作日志不确认接收请求,就像LB缓存它一样。到底是怎么回事? FYI,google LB新手。感谢



编辑:



我正在使用GCE HTTP LB。
Curl命令如下所示:



curl http://1.2.3.4 -Fkey = value-F这个curl命令在使用GCE VM IP时工作,但在使用GCE HTTP LB时不工作。 IP。

解决方案

这一行代码为我修复:

  curl_setopt($ ch,CURLOPT_HTTPHEADER,['Expect:']); 

你显然需要添加空的 Expect: header to any other headers you're sent up,but that header is what the cURL for use with Google HTTP load balancers。



更多信息



Google文档设置HTTP(S)负载平衡说明和限制部分的底部附近有一条注释,说明不支持 HTTP / 1.1 100继续响应。



默认情况下,cURL在发送POST请求时总是设置 Expect:100-continue 因此,显然cURL默认情况下无法通过GCE HTTP负载平衡器发送POST。



在最终用户端,您只会看到来自Google的502个响应因为对完全相同的POST到不在负载均衡器之后的服务器工作得很好。



但是,存在预期:100-continue 会导致Google负载平衡器崩溃并破坏请求。



在服务器端,无法解析POST数据(它甚至不到达服务器,虽然 Content-Length 被正确报告)。在我的情况下,这导致服务器返回一个500内部服务器错误,GCE LB munges并发送回用户作为一个502错误网关错误。



添加一个空的期望:标题,我的POST数据正确地,它正在我的负载平衡的VM,他们正在解析和返回有效的响应,我的客户端得到一个200而不是a 502。



感谢这个问题,帮助阐明了这个问题。


If I curl a POST request with file upload to my google compute load balancer (LB) I get a 502 error. If I do the same curl to the worker node behind the LB, it works. If I use a library like PHP Guzzle, it works either way. If I do a basic GET request on the LB, I get the correct response but the worker log does not acknowledge receiving the request as if the LB cached it. What is going on? FYI, google LB newb. Thanks

Edit:

I'm using GCE HTTP LB. The Curl command looks like this:

curl http://1.2.3.4 -F "key=value" -F "data=@path/to/file"

This curl command works when using the GCE VM IP but does NOT when using the GCE HTTP LB IP.

解决方案

This one line of code fixed it for me:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect:']);

You obviously need to add the empty Expect: header to whatever other headers you're sending up, but that header is what fixes cURL for use with Google HTTP load balancers.

More Info

The Google document Setting Up HTTP(S) Load Balancing has a note near the bottom in the Notes and Restrictions section saying that HTTP/1.1 100 Continue responses are not supported.

It seems that by default cURL will always set Expect: 100-continue headers when you send a POST request. Thus apparently cURL is unable to send POST through a GCE HTTP load balancer by default.

On the end-user side, you just see 502 responses coming back from Google, which is all the more confusing since making the exact same POST to a server that is not behind the load balancer works perfectly fine.

However, presence of Expect: 100-continue causes the Google Load Balancer to freak out and break the request.

On the server side the POST data cannot be parsed (it does not even arrive to the server, though the Content-Length is reported correctly). In my case this caused the server to return a 500 Internal Server Error, which the GCE LB munges and sends back to the user as a 502 Bad Gateway error.

After adding an empty Expect: header, my POST data is making it to my load balanced VMs correctly, they're parsing and returning valid responses, and my client is getting a 200 instead of a 502.

Thanks to this question which helped shed some light on the issue.

这篇关于Curl到Google Compute负载平衡器会收到错误502的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆