本範例示範如何針對傳統版應用程式負載平衡器,重寫要求中指定的路徑。
如要為全域外部應用程式負載平衡器和區域外部應用程式負載平衡器設定流量管理,請參閱下列頁面:
事前準備
請參閱網址重寫相關說明。
本範例假設您已按照「使用 Cloud Storage 值區設定負載平衡器」一文所述的步驟,建立外部應用程式負載平衡器。
這個範例會逐步引導您建立兩個資源:
http://IP_ADDRESS/never-fetch/three-cats.jpg
http://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」(負載平衡) 頁面 - 按一下「
http-lb
」。 - 按一下「編輯」圖示 。
- 將視窗保持開啟,以便繼續操作。
變更主機與路徑規則
- 在畫面的左欄中,按一下 [Host and path rules] (主機與路徑規則)。
- 選取「進階主機與路徑規則 (網址重新導向與重新編寫)」。
- 按一下包含非預設路徑規則的資料列,在本例中,就是所有主機上有星號 (
*
) 的資料列。 - 按一下
/love-to-fetch/* Route traffic to a single backend: dogs
列的鉛筆圖示 。 - 在「路徑」下方,刪除
/love-to-fetch/*
並新增/*
。 - 在「動作」下方,選取「將流量轉送至單一後端」。
- 按一下「附加動作 (網址重新編寫)」。
- 將「Host Rewrite」留空。
- 在「Path prefix rewrite」下方輸入
/love-to-fetch/
。 - 在「後端」下方,選取
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
。