dispatch.yaml 設定檔

區域 ID

REGION_ID 是 Google 根據您在建立應用程式時選取的地區所指派的簡寫代碼。雖然某些區域 ID 可能看起來與常用的國家/地區代碼相似,但此代碼並非對應國家/地區或省份。如果是 2020 年 2 月後建立的應用程式,App Engine 網址會包含 REGION_ID.r。如果是在此日期之前建立的現有應用程式,網址中則可選擇加入地區 ID。

進一步瞭解區域 ID

dispatch.yaml 可讓您覆寫轉送規則。您可以使用 dispatch.yaml,根據網址中的路徑或主機名稱,將傳入要求傳送至特定服務 (先前稱為模組)。

詳情請參閱要求的轉送方式

應用程式只能有一個 dispatch.yaml 檔案,且該檔案中的轉送規則會套用至所有應用程式服務和版本。路由規則也適用於 cron 檔案中使用的網址。

部署分派檔案

請在部署分派檔案之前,確保您在該檔案中定義的所有服務都已部署至 App Engine。如要部署 dispatch.yaml 檔案,請從含有 dispatch.yaml 的目錄執行 gcloud app deploy 指令:

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。