Google cloud loadbalancer Http-to-https with multiple backends

Solution 1:

You will have to add extra host and path rules to redirect http to https based on specific domain. This can be done in the google cloud console UI or using gcloud sdk.

  1. In the UI,you can edit the LB and add a "new host and path rule", and set "Hosts" and "Host redirect" to your domain. Do this for each domain.

  2. Using gcloud, you can export the URL map, modify the contents as below and upload it.

a. export existing url map -

gcloud compute url-maps export YOUR-URL-MAP --destination=/tmp/urlmap.yaml

The original yaml file will look like this for a single domain redirection web1.example.net -

name: webmap
kind: compute#urlMap
defaultUrlRedirect:
  hostRedirect: web1.example.net
  httpsRedirect: true
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  stripQuery: false

Add the second domain you want to redirect - in this case it is web2.example.net. Note the hostRules and pathMatchers section. You can add extra host rule and pathMatcher for each domain -

name: webmap
kind: compute#urlMap
defaultUrlRedirect:
  hostRedirect: web1.example.net
  httpsRedirect: true
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  stripQuery: false
hostRules:
- hosts:
  - web2.example.net
  pathMatcher: path-matcher-1
pathMatchers:
- defaultUrlRedirect:
    hostRedirect: web2.example.net
    httpsRedirect: true
    redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
    stripQuery: false
  name: path-matcher-1

You can import this file -

gcloud compute url-maps import YOUR-URL-MAP --source=/tmp/urlmap.yaml

It will take a few minutes for the change to take effect.