為什麼在調用我的 API Gateway 自訂網域名稱時傳回錯誤的憑證?

2 分的閱讀內容
0

我嘗試向我的 Amazon API Gateway 自訂網域名稱發出 API 請求,但傳回錯誤的憑證。我要如何修正此問題?

簡短描述

您可以為 API Gateway API 建立兩種類型的自訂網域名稱:Regional (區域性) 或 (僅適用於 REST API) edge-optimized (邊緣最佳化)。

對於區域性自訂網域名稱,API Gateway 會建立區域性網域名稱。您必須設定 DNS 記錄,將自訂網域名稱對應至類似於「d-example.execute-api.<region>.amazonaws.com」的主機名稱。

API Gateway 會針對邊緣最佳化的自訂網域名稱建立一個 Amazon CloudFront 分佈。您必須建立 DNS 記錄,將自訂域名對應至類似於「distribution-id.cloudfront.net」的 CloudFront 分發名稱。

在下列案例中,SSL 憑證 *.example.com 或 www.example.com 已上傳至自訂網域名稱 www.example.com。向 www.example.com 發出的 API 請求會傳回類似下列內容的錯誤:

從瀏覽器:

"This server could not prove that it is www.example.com; its security certificate is from *.execute-api.us-east-1.amazonaws.com.This may be caused by a misconfiguration or an attacker intercepting your connection."「該伺服器無法證明它是 www.example.com;它的安全憑證來自*.execute-api.us-east-1.amazonaws.com。這可能是因為設定錯誤或攻擊者攔截您的連線所致。」

從 curl:

"curl: (60) SSL: no alternative certificate subject name matches target hostname 'www.example.com'. curl failed to verify the legitimacy of the server and therefore could not establish a secure connection to it.To learn more about this situation and how to fix it, please visit the web page mentioned above."「curl:(60) SSL:沒有其他憑證主體名稱符合目標主機名稱 'www.example.com'。curl 無法驗證伺服器的合法性,因此無法建立與伺服器的安全連線。要了解有關此情況以及如何解決此問題的更多信息,請訪問上面提到的網頁。」

解決方案

請確定自訂域名的 CNAME 或 A 記錄指向 API Gateway 域名,而不是 API Gateway 階段 URL。

1.    執行下列 openssl 命令,以確認 SSL 憑證是否由自訂網域傳回:

openssl s_client -connect www.example.com:443 -servername www.example.com

輸出範例:

subject=/CN=*.execute-api.us-east-1.amazonaws.com

2.    在自訂網域上執行下列 dig 命令:

dig www.example.com

dig 命令輸出具有指向 API Gateway 階段 URL 的 CNAME 記錄,或指向 IP 地址的 A 記錄。

3.    請與您的網域註冊商一起檢查 www.example.com 網域的 DNS 記錄,以判斷該網域的指向位置。如果您的網域是透過 Amazon Route 53 註冊的,請參閱檢查來自 Route 53 的 DNS 回應

4.    若要確認 DNS 記錄已正確對應,請開啟 API Gateway 主控台。在導覽視窗中,選擇自訂網域名稱,然後選擇您的自訂網域。

5.    從 Domain details (網域詳細資訊) 中,選擇 Configuration (組態),然後檢查API Gateway domain name (API Gateway 網域名稱)。自訂網域名稱必須對應至 API Gateway 網域名稱。區域性自訂網域名稱使用類似於「d-example.execute-api.<region>.amazonaws.com」的前置詞。邊緣最佳化自訂網域名稱會使用類似於「distribution-id.cloudfront.net」的前置詞。

6.    如果 CNAME 記錄指向 API Gateway 階段 URL,而不是 API Gateway 網域名稱,則更新 CNAME 記錄。將 CNAME 記錄變更為指向您的 API Gateway 網域名稱,而非階段 URL。如果您的自訂網域名稱是向第三方註冊的,請聯絡您的網域註冊商。如果您的自訂網域名稱是透過 Route 53 註冊的,請參閱編輯記錄

7.    如果您在 Amazon Virtual Private Cloud (Amazon VPC) 端點內調用 API,則檢查自訂網域名稱的 CNAME 或 A 記錄對應。如果私人 DNS 已開啟,則 API 請求會傳回 4xx 錯誤。由於 API 要求會透過 VPC 端點路由傳送並對應至 CNAME 或 A 記錄,因此請求失敗。


相關資訊

如何為我的 API Gateway API 設定自訂網域名稱?

使用自訂網域名稱叫用 API Gateway API 時,如何解決憑證過期或「無效憑證」錯誤?

AWS 官方
AWS 官方已更新 1 年前