Asp.net

在彈性beantalk的負載均衡器中通過IIS中的url重寫重定向到https

  • November 5, 2013

當您在彈性 beanstalk 負載平衡器後面時,如何使用 IIS 的 url 重寫模組強制使用者使用 ssl?

由於幾個原因,這比聽起來更困難。一,負載均衡器負責ssl,因此從負載均衡器傳遞的請求永遠不會使用ssl。如果您使用傳統的重寫規則,您將獲得無限循環的重定向。另一個需要解決的問題是,如果 AWS 執行狀況檢查收到重定向響應,它將失敗。

  1. 解決方案的第一步是創建一個 healthcheck.html 頁面並將其設置在根目錄中。內容是什麼並不重要。
  2. 將您的負載均衡器設置為使用 healthcheck.html 文件進行健康檢查。
  3. 在您的 web.config 部分中添加以下重寫規則<system.webServer><rewrite><rules>
<rule name="Force Https" stopProcessing="true">
  <match url="healthcheck.html" negate="true" />
  <conditions>
      <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

請注意,規則匹配在我們的健康檢查文件之外的任何內容上。這可以確保負載均衡器的健康檢查成功,並且不會錯誤地將我們的伺服器從負載中刪除。

負載均衡器在標頭中傳遞 X-Forwarded-Proto 值,讓我們知道請求是否通過 https。如果該值不是 https,我們的規則將觸發並使用 https 返回永久重定向。

引用自:https://stackoverflow.com/questions/19791820