You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置GCP负载均衡器实现多域名对应多应用端口?

How to Configure GCP Load Balancer for Subdomain-to-Port Routing

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 /health that your app responds to)
  • Web2 Backend Service
    • Name: web2-backend
    • Instance Group: Same group as above
    • Port Number: 11000
    • Health Check: HTTP health check for port 11000
  • Web3 Backend Service
    • Name: web3-backend
    • Instance Group: Same group as above
    • Port Number: 12000
    • Health Check: HTTP health check for port 12000

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:

  1. Create a URL Map (e.g., web-apps-url-map)
  2. 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
    • Rule 2:
      • Hosts: web2.domain.com
      • Backend Service: web2-backend
    • Rule 3:
      • Hosts: web3.domain.com
      • Backend Service: web3-backend
  3. 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)
  • 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 (or 443 for HTTPS)
    • Target: Select your target proxy

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:01:02