跳至內容

如何使用 Application Load Balancer 將一個網域重新導向至另一個網域?

2 分的閱讀內容
0

我想使用 Application Load Balancer將流量從一個網域名稱重新導向到到另一個網域名稱。

簡短描述

若要使用 Application Load Balancer 將一個網域重新導向至另一個網域,請在 Amazon Elastic Compute Cloud (Amazon EC2) 主控台中建立接聽程式規則。Application Load Balancer 支援網網域到網域和 HTTP 到 HTTPS 重新導向。

對於此組態,最佳做法是使用 Application Load Balancer,而不是 Amazon Simple Storage Service (Amazon S3)。

解決方法

**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

若要在 Amazon EC2 主控台中建立接聽程式規則,請完成下列步驟:

  1. 開啟 Amazon EC2 console (Amazon EC2 主控台)。
  2. 在導覽窗格的 Load Balancing (負載平衡) 下,選擇 Load Balancers (負載平衡器)。
  3. 選取負載平衡器,然後選擇接聽程式
  4. 為您要使用的負載平衡器接聽程式選擇檢視/編輯規則
  5. 選擇新增規則圖示 (加號)。
  6. 選擇插入規則
  7. 選擇新增條件
  8. 在條件區段 (IF) 中,選擇新增條件。然後,完成下列步驟:
    選擇主機標頭,然後輸入您的主機名稱 (例如 example.com)。
    若要儲存,請選擇核取記號圖示。
  9. 在「動作」區段 (THEN) 中,選擇 Add action (新增動作)。然後,完成下列步驟:
    選擇 Redirect to (重新導向目標)。
    指定通訊協定和連接埠。
    Original host, path, query (原始主機、路徑、查詢) 變更為 Custom host, path, query (自訂主機、路徑、查詢)。
    Host (主機) 中,輸入 example2.com
    Path (路徑) 和 Query (查詢) 中,請保留預設值 (除非您的使用案例要求您進行變更)。
    Response (回應) 設定為 HTTP 301「永久移動」HTTP 302「已找到」
    若要儲存,請選擇
    核取記號
    圖示。
    THEN 區段現在顯示如下:
    重新導向至 https://example2.com:443/#{path}?#{query}
    狀態代碼: HTTP_301
  10. 選擇儲存

**注意:**如果兩個網域都指向相同的 Application Load Balancer,請完成下列其中一項任務:

  • 對兩個網域使用單獨的憑證。
  • 使用主體替代名稱 (SAN) 憑證來驗證網域。

若要確認重新導向是否正常運作,請完成下列步驟:

  1. 在 AWS CLI 中,使用下列 curl 函式:

    curl
        -Iv https://example.com -L
    * Rebuilt URL to: https://example.com/
    . . .
    * Connected to example.com (1.2.3.4) port 443 (#0)
    <SSL handshake>
     > Host: example.com.   ------> Host name is example.com
     > User-Agent: curl/7.61.1
     >
         Accept: */*>
     * Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
     < HTTP/2 301       ------> ALB does redirection
     < server: awselb/2.0
     < date: Fri, 06 Mar 2020 09:18:33 GMT
     < content-type: text/html
     < content-length: 150
    
     < location: https://example2.com:443/.   ----> redirected to "example2.com"
     <
     * Issue another request to this URL: 'https://example2.com:443/'. ------> Curl initiates another request that is to example2.com
     *   Trying 34.195.219.169...
    
     * TCP_NODELAY set
     <SSL handshake>
     > Host: example2.com.   ------> Host name has changed to example2.com
     > User-Agent: curl/7.61.1
     > Accept: */*
     >
     * Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
     <
         HTTP/2 200 ----> We got a response
  2. 在您的網際網路瀏覽器中,輸入 example.com 並確認其重新導向至 example2.com

**注意:**Application Load Balancer 僅支援 301 和 302 重新導向。這些重新導向允許用戶端在後續請求中將 HTTP 方法從 POST 變更為 GET。如果需要 307 重新導向,則重新導向必須透過目標應用程式進行。

相關資訊

如何使用 Amazon S3 和 Amazon Route 53 將頂點網域重新導向到其子網域或任何其他網域?

Application Load Balancer 現在支援使用 SNI 進行智慧選取的多個 TLS 憑證

AWS 官方已更新 6 個月前