Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Facing 502 errors after switching to Google Cloud Load Balancer

Hello,

Previously we had a setup where Nginx was used as a load balancer. It went as follows:

Angular app in the web (Client) --> Nginx in Compute Engine in public subnet, used as load balancer --> Application Server in private subnet, where nginx is used to route requests to the application port based on the path.

In the nginx load balancer, we were adding headers as follows to mitigate the CORS issue.

```

location /app {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type,platform,ServicePlatformID,versionid';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 200;
}
proxy_pass http://127.0.0.1:3000;
}

```

In the new setup we are using Google Cloud Load Balancer as follows:


Angular app in the web (Client) -->Google Cloud Load Balancer --> Application Server in private subnet, where nginx is used to route requests to the application port based on the path.

So we moved the above nginx configuration from load balancing compute engine to application server compute engine.

When we run the requests, it works fine via Postman but when run via the browser, we are constantly seeing the below error in Google CLB.

```
{
"insertId": "1bg7ytof0jd92",
"jsonPayload": {
"backendTargetProjectNumber": "projects/32826632432423",
"remoteIp": "124.153.49.xxx",
"statusDetails": "backend_connection_closed_before_data_sent_to_client",
"@type": "type.googleapis.com/google.cloud.loadbalancing.type.LoadBalancerLogEntry",
"cacheDecision": [
"RESPONSE_HAS_CONTENT_TYPE",
"CACHE_MODE_USE_ORIGIN_HEADERS"
]
}
```

We tried the below 2 solutions, but to no avail.

1) Set `Timeout` and `Connection Draining Timeout` to the same value(in our case, 300) as per this.

2) Set `keepalive_timeout 650s;` in Nginx as per this.

Since none of these solutions helped, we are now stuck. Would appreciate any help to fix this.

Solved Solved
1 4 2,654
1 ACCEPTED SOLUTION

After lots of experimentation, I was able to resolve the issue.

In the nginx configuration

 
```

location /app {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type,platform,ServicePlatformID,versionid';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 200;
}
proxy_pass http://127.0.0.1:3000;
}

```
 
Replacing return 200; with return 204; fixed the issue. I'm not sure why this is the case as return 200; works for both nginx as well as aws load balancer that we use in other applications, but after lots of trail and error, this is the conclusion I've reached.

View solution in original post