dispatch.yaml 配置文件

区域 ID

REGION_ID 是 Google 根据您在创建应用时选择的区域分配的缩写代码。此代码不对应于国家/地区或省,尽管某些区域 ID 可能类似于常用国家/地区代码和省代码。对于 2020 年 2 月以后创建的应用,REGION_ID.r 包含在 App Engine 网址中。对于在此日期之前创建的现有应用,网址中的区域 ID 是可选的。

详细了解区域 ID

dispatch.yaml 可以替换路由规则。您可以使用 dispatch.yaml 根据网址中的路径或主机名向特定服务(以前称为模块)发送传入请求。

如需了解详情,请参阅请求的路由方式

一个应用只能有一个 dispatch.yaml 文件,该文件中的路由规则将应用于应用的所有服务和版本。

部署调度文件

要将调度文件中的配置部署并应用到 App Engine 环境中,请运行以下命令:

gcloud app deploy dispatch.yaml

语法

dispatch.yaml 文件中的根元素是 dispatch:,其中包含由以下子元素指定的路由定义列表。

您在调度文件中定义的规则必须使用 HTTP 网址格式,这种格式采用“.”表示法分隔子网域。不支持使用 HTTPS“-dot-”表示法定义的网址。

调度规则按顺序执行,并且将仅应用与网址匹配的第一项规则。

元素 说明
service

指定服务的名称,该服务将处理与 url 格式匹配的请求。请注意,服务以前称为模块。

url

url 元素中,您可在英文引号内定义网址该格式可包含主机名和网址路径且长度不超过 100 个字符。

对于 service 元素,请指定您希望用于处理传入请求的服务的名称,其中这些请求需要与 url 元素的网址格式匹配。

提示:您可以在 url 元素中包含 * 通配符等 Glob 格式;但是,这些格式只能在主机名的前面和网址路径的末尾使用。

调度文件不会路由以 /_ah/ 开头的网址路径。

示例

下面的示例调度文件会将请求路由到 https://simple-sample.uc.r.appspot.com,并将 https://simple-sample.uc.r.appspot.com/favicon.ico 等请求路由到 default 服务。所有静态内容均由 default 服务提供。像 https://simple-sample.uc.r.appspot.com/mobile/ 这样的移动请求会路由到移动前端,而像 https://simple-sample.uc.r.appspot.com/work/ 这样的工作器请求会路由到静态后端。

示例:

dispatch:
  # Default service serves the typical web resources and all static resources.
  - url: "*/favicon.ico"
    service: default

  # Default service serves simple hostname request.
  - url: "simple-sample.uc.r.appspot.com/"
    service: default

  # Send all mobile traffic to the mobile frontend.
  - url: "*/mobile/*"
    service: mobile-frontend

  # Send all work to the one static backend.
  - url: "*/work/*"
    service: static-backend

如果您更倾向于使用可与大量可能的请求相匹配的宽泛路由规则,则可以定义范围更广泛的规则。

示例:

# Send any path that begins with “simple-sample.uc.r.appspot.com/mobile” to the mobile-frontend service.
- url: "simple-sample.uc.r.appspot.com/mobile*"
  service: mobile-frontend

# Send any domain/sub-domain with a path that starts with “work” to the static backend service.
- url: "*/work*"
  service: static-backend

您也可以编写更严格的表达式。

示例:

# Matches the path "/fun", but not "/fun2" or "/fun/other"
- url: "*/fun"
  service: mobile-frontend

# Matches the hostname 'customer1.myapp.com', but not '1.customer1.myapp.com.
- url: "customer1.myapp.com/*"
  service: static-backend

您可以创建规则,将传入的网域请求重定向到服务。以下规则将来自“customer1.myapp.com”的传入请求路由到默认服务,并将来自子网域的传入请求路由到静态后端服务。

示例:

# Matches the domain name 'customer1.myapp.com' and directs all the request to default service
- url: "customer1.myapp.com/*"
  service: default

# Matches all the subdomains of 'customer1.myapp.com' and directs all the request to static-backend service
- url: "*.customer1.myapp.com/*"
  service: static-backend

限制

调度文件最多可包含 20 个路由规则。指定网址字符串时,主机名和路径的长度均不能超过 100 个字符。

删除所有调度规则

要删除所有调度规则,请执行以下操作:

  1. 如下所示修改 dispatch.yaml 文件的内容:

    dispatch: []
    
  2. dispatch.yaml 文件部署到 App Engine。