Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect all HTTP requests to HTTPS with GCP Load Balancer

I've setup the standard GCP load balancer to point to my instance group. It talks over the same port on the instance. I would like to redirect http to https. I would normally do this in nginx or apache on the instance but that won't work since its https already from the load balancer.

Is there a way to rewrite the url similar to if I was using nginx and apache to load balance in GCP's Load Balancer? or should I forward http and https to the instance and have the instance handle the rewrite as I normally would. I'm new to GCP thanks in advance.

like image 773
Jared Avatar asked May 17 '16 05:05

Jared


People also ask

How can I redirect HTTP requests to HTTPS using an application load balancer?

Select a load balancer, and then choose HTTP Listener. Under Rules, choose View/edit rules. Choose Edit Rule to modify the existing default rule to redirect all HTTP requests to HTTPS. Or, insert a rule between the existing rules (if appropriate for your use case).

Can classic load balancer redirect HTTP to HTTPS?

Classic Load Balancers can't redirect HTTP traffic to HTTPS by default. Instead, configure your rewrite rules for the web servers instances behind the Classic Load Balancer. You must configure your rewrite rules to use the X-Forwarded-Proto header and redirect only HTTP clients.


1 Answers

You can set it up the same way as Nginx does. When you see traffic on a port which is not https, you redirect it to HTTPs.

To do this, you can use X-Forwarded-Proto header which contains the protocol using which the traffic came in. On your server, you can simply look for traffic that has http header and upgrade that request to HTTPS.

Most commonly used way is to use 301 redirect, but that is not a great practice. One should use HTTP 426 upgrade request header.

Read more: Is HTTP status code 426 Upgrade Required only meant signal an upgrade to a secure channel is required?

RFC doc: https://www.rfc-editor.org/rfc/rfc2616#section-14.42

like image 87
Vikram Tiwari Avatar answered Sep 20 '22 12:09

Vikram Tiwari