本範例示範如何為傳統版應用程式負載平衡器,重新編寫要求中指定的路徑。
如要設定全域外部應用程式負載平衡器和區域外部應用程式負載平衡器的流量管理功能,請參閱下列頁面:
事前準備
請參閱網址重寫。
網址重寫範例假設您已按照「使用 Cloud Storage bucket 設定負載平衡器」一文所述步驟,建立外部應用程式負載平衡器。
該範例會引導您建立兩項資源:
http://IP_ADDRESS/never-fetch/three-cats.jpghttp://IP_ADDRESS/love-to-fetch/two-dogs.jpg
其中
/never-fetch/three-cats.jpg儲存在/cats(預設服務),而/love-to-fetch/two-dogs.jpg儲存在/dogs。此時,網址對應看起來會像這樣:
gcloud compute url-maps describe http-lb
creationTimestamp: '2020-10-13T11:18:10.561-07:00' defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats fingerprint: MKfYsObzqgw= hostRules: - hosts: - '*' pathMatcher: path-matcher-1 id: '1420501688756228493' kind: compute#urlMap name: test-bucket pathMatchers: - defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats name: path-matcher-1 pathRules: - paths: - /love-to-fetch/* service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogs selfLink: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/http-lb
修改網址對應
在本範例中,您會重新編寫 /love-to-fetch/ 網址,讓使用者能透過這個簡化網址存取 two-dogs.jpg 圖片:
http://IP_ADDRESS/two-dogs.jpg
如要執行這項操作,請按照下列步驟修改負載平衡器的網址對應:
控制台
編輯負載平衡器
- 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。
前往「Load balancing」(負載平衡) 頁面 - 按一下「
http-lb」。 - 按一下「Edit」(編輯)。
- 視窗保持開啟,以便繼續操作。
變更主機與路徑規則
- 在畫面的左欄中,按一下 [Host and path rules] (主機與路徑規則)。
- 選取「進階型主機與路徑規則 (網址重新導向與網址重新編寫)」。
- 按一下包含非預設路徑規則的資料列,在本例中,是所有主機都使用星號 (
*) 的資料列。 - 按一下「」列的鉛筆圖示
/love-to-fetch/* Route traffic to a single backend: dogs。 - 在「Paths」下方刪除
/love-to-fetch/*,並新增/*。 - 在「動作」下方,選取「將流量轉送至單一後端」。
- 按一下「附加動作 (網址重新編寫)」。
- 將「主機重新編寫」留空。
- 在「路徑前置字串重新編寫」下方輸入
/love-to-fetch/。 - 在「後端」下方,選取
dogs。 - 按一下 [儲存]。
按一下「完成」。 新的主機與路徑規則如下所示:
路徑 動作 後端 任何不相符的項目 (預設) 將流量轉送至單一後端 cats /* 將流量轉送至單一後端 dogs 若一切正確,請按一下「Update」(更新),即可更新您的 HTTP 負載平衡器。
gcloud
建立 YAML 檔案
/tmp/http-lb.yaml,並將 PROJECT_ID 替換為您的專案 ID。使用者要求路徑
/*時,後端會將路徑重新編寫為內容的實際位置/love-to-fetch/*。defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats hostRules: - hosts: - '*' pathMatcher: path-matcher-1 name: http-lb pathMatchers: - defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/cats name: path-matcher-1 pathRules: - paths: - /* routeAction: urlRewrite: pathPrefixRewrite: /love-to-fetch/ service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogs tests: - description: Test routing to backend bucket, dogs host: example.com path: /love-to-fetch/test service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/dogs驗證網址對應。
gcloud compute url-maps validate --source /tmp/http-lb.yaml
如果測試通過且指令輸出成功訊息,請將變更儲存至網址對應。
更新網址對應。
gcloud compute url-maps import http-lb \ --source /tmp/http-lb.yaml \ --global
測試
請記下預留的 IPv4 位址:
gcloud compute addresses describe example-ip \
--format="get(address)" \
--global
等待幾分鐘讓變更生效後,即可測試這項設定。
在指令列中執行下列 curl 指令:
curl http://IP_ADDRESS/two-dogs.jpg
在瀏覽器中開啟 http://IP_ADDRESS/two-dogs.jpg。