Google Cloud Platform: redirecting HTTP traffic to HTTPS

We’re in the process of moving all our projects on Kubernetes to make use of GCE (Google Compute Engine) ingress instead of the Nginx ingress, to be more tightly integrated with the whole Google Cloud eco-system.

But unfortunately, it’s not possible at the moment to redirect HTTP traffic straight to HTTPS 😦. Yes, you heard it right. This seemingly basic feature is not supported straight of the box.

If we read the docs on Github (https://github.com/kubernetes/ingress-gce).

To redirect traffic from `:80` to `:443` you need to examine the `x-forwarded-proto` header inserted by the GCE L7 since the Ingress does not support redirect rules. In Nginx, this is as simple as adding the following lines to your config:

# Replace '_' with your hostname.
server_name _;
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}

It’s sad that we need to do this manually but luckily we still have Nginx to the rescue.