在使用 GCP 負載平衡器設定 IAP 的情況下,如何從 HTTP 重新導向到 HTTPS?

在使用 GCP 負載平衡器設定 IAP 的情況下,如何從 HTTP 重新導向到 HTTPS?

我目前有一個託管在 Google Compute Engine 上的網站,該網站透過位於負載平衡器後面的 Identity-Aware-Proxy 進行了身份驗證。這一切都在 https 上運行得很好,但我想確保 http 重定向到 https,因為它目前只回應 404。

於是,我跟著https://cloud.google.com/load-balancing/docs/https/setting-up-http-https-redirect它告訴您設定第二個負載平衡器以將 http 流量重新導向到 https。

然而問題是,按照這些說明進行操作後,當我瀏覽到http://my-website.com我收到以下錯誤:

錯誤 403(禁止)!

  1. 這是一個錯誤。

您的客戶端無權從該伺服器取得 URL /。我們知道的就這些。

儘管 http 負載平衡器設定了 301 - 永久移動完整路徑重定向,但在瀏覽器開發人員工具網路標籤中沒有發生重定向。它只是立即回應 403。


總而言之,我的設定如下:

外部 HTTPS 負載平衡器

  • 前端 - HTTPS、靜態 IP、僅限 HTTPS
  • 後端 - Identity-Aware-Proxy(透過 Identity Platform 進行電子郵件驗證)-> Compute Engine 實例群組

外部 HTTP 負載平衡器

  • 前端 - HTTP、靜態 IP(與上面的 HTTPS 負載平衡器相同)
  • 後端 - 無
  • 主機和路徑規則:
    • 模式:進階主機和路徑規則(URL 重定向、URL 重寫)
    • 行動:將客戶端重新導向到不同的主機/路徑
    • 主機重定向: https://my-website.com
    • 路徑值:*
    • 重定向回應代碼:301 - 永久移動
    • HTTPS 重新導向:啟用

任何如何解決這個問題而不得到 403 的想法將不勝感激!

答案1

按照你描述的方式看來您的 url-map 有問題因為您可以造訪http您網站的版本。

為了絕對確定,請仔細檢查(或創建新的) url-mapgcloud指令:

gcloud compute url-maps describe web-map-http

creationTimestamp: '2020-12-02T03:18:27.053-08:00'
defaultUrlRedirect:
  httpsRedirect: true
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
fingerprint: KU2hPu1ao=
id: '50586028237895404'
kind: compute#urlMap
name: web-map-http
selfLink: https://www.googleapis.com/compute/v1/projects/xxxxx/global/urlMaps/web-map-http

準備好 URL 映射後,建立一個目標代理:

gcloud compute target-http-proxies create http-lb-proxy \
   --url-map=web-map-http \
   --global

結果:

gcloud compute target-http-proxies describe http-lb-proxy
creationTimestamp: '2020-12-02T03:19:39.090-08:00'
fingerprint: lgJkIY8E=
id: '3781119498457764'
kind: compute#targetHttpProxy
name: http-lb-proxy
selfLink: https://www.googleapis.com/compute/v1/projects/xxxx/global/targetHttpProxies/http-lb-proxy
urlMap: https://www.googleapis.com/compute/v1/projects/xxxx/global/urlMaps/web-map-http

和轉送規則:

gcloud compute forwarding-rules create http-content-rule \
   --address=lb-ipv4-1 \ # Same IP address used for HTTPS load balancer
   --global \
   --target-http-proxy=http-lb-proxy \
   --ports=80

它應該看起來像:

gcloud compute forwarding-rules describe http-content-rule --global
IPAddress: 34.107.123.141
IPProtocol: TCP
creationTimestamp: '2020-12-02T03:22:38.132-08:00'
description: ''
fingerprint: L1vA0Ik9Y=
id: '888330202637841'
kind: compute#forwardingRule
loadBalancingScheme: EXTERNAL
name: http-content-rule
networkTier: PREMIUM
portRange: 80-80
selfLink: https://www.googleapis.com/compute/v1/projects/xxxx/global/forwardingRules/http-content-rule
target: https://www.googleapis.com/compute/v1/projects/xxxx/global/targetHttpProxies/http-lb-proxy

確保對 HTTP 和 HTTPS LB 使用相同的公用 IP。如果傳入流量沒有被阻止,請檢查防火牆規則。

如果一切正確,您應該得到curl與範例中所示相同的輸出。

相關內容