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

Configure HTTP headers, in the external application loadbalancer

In GCP, I am running three NodeJS APIs in a single instance with three different port numbers (3000, 3001, and 3002), and I created an external application load balancer using the HTTPS front end. I also created a backend service for the three port numbers. The load balancer is attached to the instance group.

If I am requesting the URL "https://example.com/" reaching port 3000 This is working well for me for the single-backend service. but I need to pass this same URL, "https://example.com/" with different HTTP headers and values. For example, if I request the URL "https://example.com/" port 3000 needs to be reached, but if I request the same URL "https://example.com/" with the "mydealsapi" HTTP headers, port 3001 needs to be reached, and again, if I request the same URL "https://example.com/" with the "appapi" HTTP headers, port 3002 needs to be reached.

1 1 1,980
1 REPLY 1

Hi @NandhaGCP ,

Understanding your scenario, you want to route traffic based on different HTTP headers:

  • If the request has the header mydealsapi, route to Node.js API on port 3000.
  • If the request has the header appapi, route to Node.js API on port 3001.
  • If the request has both headers mydealsapi and appapi, route to Node.js API on port 3002.
  • If the request doesn't have either header, route to Node.js API on port 3000 (default).|

1. You have to create a backend services for each port:

gcloud compute backend-services create backend-service-3000 \
--protocol HTTP --port-name http --port 3000

gcloud compute backend-services create backend-service-3001 \
--protocol HTTP --port-name http --port 3001

gcloud compute backend-services create backend-service-3002 \
--protocol HTTP --port-name http --port 3002

2.  Create URL Map:

gcloud compute url-maps create my-url-map

3. Then, create Path Matchers for each port:

gcloud compute url-maps add-path-matcher my-url-map \
--default-service backend-service-3000 \
--path-matcher-name path-matcher-3000 \
--new-hosts=example.com \
--new-prefixes=/

gcloud compute url-maps add-path-matcher my-url-map \
--default-service backend-service-3001 \
--path-matcher-name path-matcher-3001 \
--new-hosts=example.com \
--new-prefixes=/

gcloud compute url-maps add-path-matcher my-url-map \
--default-service backend-service-3002 \
--path-matcher-name path-matcher-3002 \
--new-hosts=example.com \
--new-prefixes=/

4. Configure header actions :

gcloud compute url-maps add-header-action my-url-map \
--header-action-name header-action-3000 \
--request-headers=mydealsapi \
--path-matcher-name path-matcher-3000

gcloud compute url-maps add-header-action my-url-map \
--header-action-name header-action-3001 \
--request-headers=appapi \
--path-matcher-name path-matcher-3001

gcloud compute url-maps add-header-action my-url-map \
--header-action-name header-action-3002 \
--request-headers=mydealsapi,appapi \
--path-matcher-name path-matcher-3002

5. Create a target proxy :

gcloud compute target-http-proxies create my-target-proxy \
--url-map=my-url-map

6. Global forwarding rule :

gcloud compute forwarding-rules create my-forwarding-rule \
--global \
--target-http-proxy=my-target-proxy \
--ports=443 \
--load-balancing-scheme=EXTERNAL

This setup should route traffic based on the specified HTTP headers to the appropriate Node.js API running on ports 3000, 3001, or 3002.