使用 IIS ARR 作為反向代理伺服器 (Reverse Proxy)


  1. 說明
    1. Installation
    2. Setup
    3. 心得
  2. 參考資料

說明如何使用 IIS ARR 作為反向代理伺服器。

logo

說明

Installation

前往 Microsoft Application Request Routing 3.0 (x64) 進行下載,前置必須先安裝 URL Rewirte

URL Rewrite 的安裝檔在連結的下方有分語言提供 x86 以及 x64 installer;如果直接使用 Install this extension 會使用 Web Platform 進行安裝,對於不對外連線的 Server 而言,無法透過 Install this extension 安裝。

Setup

Application Rqeuest Routing Cache In Server Node
Click Server Proxy Settings
Enable Proxy
URL Rewrite
Edit URL Rewrite with GUI and Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <outboundRules>
                <rule name="tour-1" preCondition="IsHTML">
                    <match filterByTags="Img, Link, Script" pattern="assets/(.*)" />
                    <conditions>
                        <add input="{URL}" pattern="^/tour/?.*" />
                    </conditions>
                    <action type="Rewrite" value="/tour/assets/{R:1}" />
                </rule>
                <rule name="tour-2" preCondition="IsHTML">
                    <match filterByTags="A" pattern="(.*html)" />
                    <conditions>
                        <add input="{URL}" pattern="^/tour/?.*" />
                    </conditions>
                    <action type="Rewrite" value="{R:1}/" />
                </rule>
                <preConditions>
                    <preCondition name="IsHTML">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>

            <rules>
                <rule name="Reverse Proxy to Tour" stopProcessing="true">
                    <match url="tour/?(.*)" />
                    <action type="Rewrite" url="https://tour.sdwh.dev/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to intranet" stopProcessing="true">
                    <match url="www/(.*)" />
                    <action type="Rewrite" url="http://192.168.112.152/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

心得

ARR 在反向代理不如 Nginx 討論度高,但作為微軟生態系以及解決方案,更容易整合在 Windows Server 環境的特點,都是讓它成為不少採用 Windows 的需求方使用反向代理的首選。

而儘管 URL Rewrite 不如 Nginx 的 conf 設定來得直觀,甚至 GUI 各種選項都要測試過才能明白字面的說明是什麼意思,但 URL Rewrite 原生支援 Regex Match Pattern 的方式,增加了學習的門檻卻也提升了深度使用的潛力。

參考資料

Reverse Proxy with URL Rewrite v2 and Application Request Routing - learn.microsoft

黑暗執行緒 - 【茶包射手日記】在 ASP.NET MVC 站台使用 IIS ARR

Will 保哥 - 如何利用 IIS7 的 ARR 模組實做 Reverse Proxy 機制

Secret Note - 設定Reverse Proxy在Windows、Mac