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

如何在Google App Engine中为每个服务绑定独立域名/子域名

解决GAE多域名映射到不同服务的问题

我来帮你搞定这个域名映射的问题——其实关键是你没找到在同一个域名下指定不同服务的入口,咱们一步步来:

首先先确认你的两个服务状态:Go API是默认服务(没有指定service字段),Angular是stage服务,已经都部署成功了对吧?

步骤1:确认主域名已添加到GAE项目

你已经配置了api.domain.com,那domain.com大概率已经添加到「App Engine > Settings > Custom Domains」页面里了,但还是要检查一下:打开这个页面,看看有没有domain.com的条目,如果没有的话,先点击「Add a custom domain」完成域名验证(和你添加api.domain.com的流程一样)。

步骤2:为stage服务创建主域名映射

这是你找不到的关键步骤:

  1. 在Custom Domains页面,找到domain.com的条目,点击它旁边的「Add mapping」按钮
  2. 在弹出的配置窗口里:
    • 服务:选择stage(就是你的Angular应用所在的服务)
    • 子域名:留空或者输入@(这样就会映射主域名domain.com)
    • SSL证书:选你已经有的证书,或者让GAE自动生成(如果还没有的话)
  3. 点击「Save」完成配置

步骤3:验证DNS和服务访问

  • 检查你的DNS配置:domain.com的A/AAAA记录要指向GAE提供的IP地址,CNAME记录(如果用的话)要指向ghs.googlehosted.com——和你配置api.domain.com的DNS规则一致就行
  • 等待DNS生效(通常需要5-30分钟,慢的话可能几小时),然后访问domain.com测试是否能加载Angular应用,api.domain.com是否正常返回Go API的响应

额外注意点

  • 确保stage服务部署状态正常,没有报错
  • 你的Angular app.yaml里设置了secure: always,所以访问HTTP会自动跳转到HTTPS,这没问题,但要确保SSL证书覆盖了domain.com
  • 如果访问后还是有问题,可以用curl -v domain.com查看响应头,确认请求是否路由到了stage服务

附上你提供的两个app.yaml的格式化版本,方便参考:

Go API应用的app.yaml

runtime: go
api_version: go1.9
handlers:
- url: /.*
  script: _go_app

Angular应用的app.yaml

service: stage
runtime: python27
api_version: 1
threadsafe: true
skip_files:
- ^(?!dist) # Skip any files not in the dist folder
handlers:
# Routing for bundles to serve directly
- url: /((?:inline|main|polyfills|styles|vendor)\.[a-z0-9]+\.bundle\.js)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*
# Routing for a prod styles.bundle.css to serve directly
- url: /(styles\.[a-z0-9]+\.bundle\.css)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*
# Routing for typedoc, assets and favicon.ico to serve directly
- url: /((?:assets|docs)/.*|favicon\.ico)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*
# Any other requests are routed to index.html for angular to handle so we don't need hash URLs
- url: /.*
  secure: always
  redirect_http_response_code: 301
  static_files: dist/index.html
  upload: dist/index\.html
  http_headers:
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    X-Frame-Options: DENY

内容的提问来源于stack exchange,提问作者tom

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 03:58:07