如何配置GCP负载均衡器实现多域名对应多应用端口?
Got it, let's walk through exactly how to set up your GCP Load Balancer to route each subdomain to the corresponding port on your servers. This relies on GCP's HTTP(S) Load Balancer, which supports host header matching—perfect for your use case.
Step 1: Create Backend Services (One Per Application)
First, we need to define a backend service for each of your web apps. Each service will target your three servers (Serv1/Serv2/Serv3) on their specific port:
- Web1 Backend Service
- Name:
web1-backend - Instance Group: Select the group containing Serv1, Serv2, Serv3 (create one if you haven’t already)
- Port Number:
10000 - Health Check: Create an HTTP health check targeting port
10000(use a simple path like/healththat your app responds to)
- Name:
- Web2 Backend Service
- Name:
web2-backend - Instance Group: Same group as above
- Port Number:
11000 - Health Check: HTTP health check for port
11000
- Name:
- Web3 Backend Service
- Name:
web3-backend - Instance Group: Same group as above
- Port Number:
12000 - Health Check: HTTP health check for port
12000
- Name:
Pro tip: Make sure your servers’ firewall rules allow incoming traffic from GCP’s health check IP ranges and client IPs on ports 10000, 11000, 12000.
Step 2: Set Up a URL Map (Route Subdomains to Backends)
This is where we tell the load balancer to map each subdomain to its corresponding backend service:
- Create a URL Map (e.g.,
web-apps-url-map) - Under Host Rules, add three rules:
- Rule 1:
- Hosts:
web1.domain.com - Path Matcher: Select "Default Path Matcher" (since we’re routing the entire subdomain, no specific paths needed)
- Backend Service:
web1-backend
- Hosts:
- Rule 2:
- Hosts:
web2.domain.com - Backend Service:
web2-backend
- Hosts:
- Rule 3:
- Hosts:
web3.domain.com - Backend Service:
web3-backend
- Hosts:
- Rule 1:
- Optional: Set a default backend (e.g., a 404 page) for any unmatched traffic.
Step 3: Configure Target Proxy and Forwarding Rule
Next, we need to expose the URL Map to the internet:
- Target HTTP Proxy (or Target HTTPS Proxy if using SSL):
- Name:
web-apps-proxy - URL Map: Select
web-apps-url-map - For HTTPS: Attach a managed SSL certificate (GCP offers free managed certs for your domains)
- Name:
- Forwarding Rule:
- Name:
web-apps-forwarding-rule - IP Version: IPv4 (or IPv6 if needed)
- IP Address: Choose a static IP (recommended, so you can easily map your domains to it)
- Port Range:
80(or443for HTTPS) - Target: Select your target proxy
- Name:
Step 4: Update Domain DNS Records
Finally, point your subdomains to the load balancer’s static IP:
- Create A records (or AAAA for IPv6) for
web1.domain.com,web2.domain.com,web3.domain.com - Set each record’s value to the static IP you assigned to the forwarding rule.
That’s it! Once everything propagates, each subdomain will route to the correct port across your three servers.
内容的提问来源于stack exchange,提问作者lvandyk

