Search code examples
google-cloud-platformgoogle-compute-enginegoogle-cloud-cdn

Redirect based on "Accept-Language" request header leads to error on Google Cloud CDN


I am currently setting up an Nginx server on a "Google Compute Engine" behind Google's Load Balancer/CDN combo:

Website visitor <---> CDN <---> Load Balancer <---> Nginx on Google Compute Engine

I would like to redirect the visitor from https://www.example.org/ to either https://www.example.org/de/ or https://www.example.org/en/ depending on the value of the "Accept-Language" HTTP-Header in the client's request. For this purpose, I am using the following code in the nginx.conf configuration file:

set $language_suffix "en";
if ($http_accept_language ~* "^de") {
  set $language_suffix "de";
}

location = / {
  add_header Vary "Accept-Language";
  return 303 https://www.example.org/$language_suffix/;
}

But, above config leads to a 502 error:

~> curl -I https://www.example.org/
HTTP/2 502 
content-type: text/html; charset=UTF-8
referrer-policy: no-referrer
content-length: 332
date: Mon, 11 Jun 2018 09:57:55 GMT
alt-svc: clear

How can I fix this?

UPDATE:

XXX.XXX.XXX.XXX - "HEAD https://www.XXXXXXX.com/" 502 106 "curl/7.60.0" {
 httpRequest: {
  cacheLookup:  true   
  remoteIp:  "XXX.XXX.XXX.XXX"   
  requestMethod:  "HEAD"   
  requestSize:  "38"   
  requestUrl:  "https://www.XXXXXXX.com/"   
  responseSize:  "106"   
  status:  502   
  userAgent:  "curl/7.60.0"   
 }
 insertId:  "XXXXXXXXXXXXX"  
 jsonPayload: {
  @type:  "type.googleapis.com/google.cloud.loadbalancing.type.LoadBalancerLogEntry"   
  statusDetails:  "failed_to_pick_backend"   
 }
 logName:  "projects/crack-triode-XXXXXXXX/logs/requests"  
 receiveTimestamp:  "2018-06-11T03:33:10.864056419Z"  
 resource: {
  labels: {
   backend_service_name:  ""    
   forwarding_rule_name:  "XXX-werbserver-ipv4-https"    
   project_id:  "crack-triode-XXXXXXXX"    
   target_proxy_name:  "XXX-werbserver-loadbalancer-target-proxy-2"    
   url_map_name:  "XXX-werbserver-loadbalancer"    
   zone:  "global"    
  }
  type:  "http_load_balancer"   
 }
 severity:  "WARNING"  
 spanId:  "XXXXXXXXXXXXXX"  
 timestamp:  "2018-06-11T03:33:10.088466141Z"  
 trace:  "projects/crack-triode-XXXXXXXX/traces/XXXXXXXXXXXXXXX"  
}

Solution

  • You have to change the request uri from / to some else, that returns HTTP-Status 200. I am now using /robots.txt. The setting can be changed at:

    https://console.cloud.google.com/compute/healthChecks

    enter image description here