GCP Load Balancer: 502 Server Error, "failed_to_connect_to_backend"

7/23/2018

I have a dockerized Go application running on two GCP instances, everything works fine when using them with their individual external IPs, but when put through the load balancer, they're either slow to answer or it answers a 502 server error. The health checks seems to be ok, so I really don't understand.

In the logs, the error thrown is

failed_to_connect_to_backend

I've already seen other answers on this question, but none of them seems to provide an answer for my case. I cannot modify the way the application is served, so it doesn't seems to be a timeout thing.

-- navelencia
docker
go
google-cloud-platform
google-kubernetes-engine
load-balancing

3 Answers

5/12/2020

this happened to me more than once, I was using apache in my servers, and the issue was not of CPU, but of configuration,

I am using apache mpm_event in combination with php-fpm and there are many settings that will limit the max amount of requests that you want apache and fpm to allow.

In my case I increased in Apache MPM config MaxRequestWorkers from the default 150 to 600, and in PHP FPM config pm.max_children to 80 (I don't remember what was the default here)

This worked as expected, hope this helps you to extrapolate to your own stack.

-- santiago arizti
Source: StackOverflow

6/27/2019

checking whether your backend block google's cloud cdn ip address or not.those addresses can be found here:https://cloud.google.com/compute/docs/faq#find_ip_range

-- Harris Tailor
Source: StackOverflow

7/26/2018

To troubleshoot 502 response from the Load Balancer due to "failed_to_connect_to_backend." I would check the followings:

1) Usually, "failed_to_connect_to_backend" error message indicates that the load balancer is failing to connect to backends, investigating URL map rules is also a good point to start. I would also suggest reviewing your Load Balancer's URL map to make sure that Host rules, Path matcher, and Path rules are correctly defined and comply with descriptions in this article.

2) Also check if the backend instances are exhausting their resources, If a backend server is overwhelmed, it will refuse incoming requests, potentially causing the load balancer to give up on it and return the specific 502 error you're experiencing. For Apache, you could use this link and nginx this link. Also, check the output on how many established connections are present at any one time using 'netstat' and watch command.

3) I would also recommend testing again with the HTTP(S) request directly to the instance, request the same URL that reporting 502. You might do this test in another VM instance in your VPC network.

-- Nur
Source: StackOverflow