萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

IIS 7 Application Request Routing (ARR) 反向代理配置圖文完整教學

場景

  • 單一公網 IP (192.168.1.8),需代理多個內部站點:
    phpweb.leven.com.cn → localhost:8081 (PHP)
    levenblog.leven.com.cn → localhost:8080 (LevenBlog)
    realblog.leven.com.cn → leven.com.cn (公網站點)
    localhost/leven → leven.com.cn (路徑代理)

前置準備

  1. 下載安裝 Application Request Routing (ARR) v2+:
    官網下載
  2. 安裝 URL Rewrite Module 2.0+ (ARR 依賴)。
  3. IIS 管理員 → 伺服器節點 → Application Request Routing Cache → Server Proxy Settings → 勾選 Enable proxy。
    啟用代理

建立前端網站 (LevenWeb, Port 80)

  1. 新增網站 LevenWeb,綁定 Port 80、主機名留空 (預設網站)。
  2. 在該網站下配置 URL Rewrite 規則 (可用 UI 或直編 web.config)。

規則 1:域名代理至本地 PHP 站點

UI 配置步驟

  1. 選擇 Add Rule(s) → Blank Rule。
    空白規則
  2. Name: phpweb,Pattern: ^(.*)$。
    模式
  3. Conditions: Add → {HTTP_HOST} Matches ^phpweb.leven.com.cn$。
    條件
  4. Action: Rewrite → https://localhost:8081/{R:1}。
    動作

等效 web.config

<rewrite>
  <rules>
    <rule name="phpweb">
      <match url="^(.*)$" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^phpweb.leven.com.cn$" />
      </conditions>
      <action type="Rewrite" url="https://localhost:8081/{R:1}" />
    </rule>
  </rules>
</rewrite>

規則 2/3:同理配置 levenblog 與 realblog

<rule name="levenblog">
  <match url="^(.*)$" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^levenblog.leven.com.cn$" />
  </conditions>
  <action type="Rewrite" url="https://localhost:8080/{R:1}" />
</rule>
<rule name="realblog">
  <match url="^(.*)$" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^realblog.leven.com.cn$" />
  </conditions>
  <action type="Rewrite" url="https://leven.com.cn/{R:1}" />
</rule>

規則 4:路徑代理 (localhost/leven → 公網站點) 與 Outbound 重寫

問題

代理後 CSS/JS/圖片/連結路徑變為 /leven/xxx,但上游站點輸出絕對路徑 /xxx,導致資源 404、樣式錯亂。
樣式錯亂
原始碼路徑錯誤

Inbound Rule (路徑代理)

<rule name="leven.com.cn">
  <match url="^leven/(.*)$" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^localhost$" />
  </conditions>
  <action type="Rewrite" url="https://leven.com.cn/{R:1}" />
</rule>

Outbound Rule (回應內容路徑重寫)

  1. IIS → URL Rewrite → Outbound Rules → Add Rule → Blank Rule。
    出站規則
  2. Name: Add application prefix。
    Match: filterByTags="A,Img,Script,Link" pattern="^/(.*)$"。
    Conditions: {URL} Matches ^/leven/.*。
    Action: Rewrite → /leven/{R:1}。

等效 web.config

<outboundRules>
  <rule name="Add application prefix">
    <match filterByTags="A,Img,Script,Link" pattern="^/(.*)$" />
    <conditions>
      <add input="{URL}" pattern="^/leven/.*" />
    </conditions>
    <action type="Rewrite" value="/leven/{R:1}" />
  </rule>
</outboundRules>

效果驗證

重新整理頁面,CSS/JS/圖片/連結全部正常載入。
修正後

完整 web.config 範例

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="phpweb"><match url="^(.*)$" /><conditions><add input="{HTTP_HOST}" pattern="^phpweb.leven.com.cn$" /></conditions><action type="Rewrite" url="https://localhost:8081/{R:1}" /></rule>
        <rule name="levenblog"><match url="^(.*)$" /><conditions><add input="{HTTP_HOST}" pattern="^levenblog.leven.com.cn$" /></conditions><action type="Rewrite" url="https://localhost:8080/{R:1}" /></rule>
        <rule name="realblog"><match url="^(.*)$" /><conditions><add input="{HTTP_HOST}" pattern="^realblog.leven.com.cn$" /></conditions><action type="Rewrite" url="https://leven.com.cn/{R:1}" /></rule>
        <rule name="leven.com.cn"><match url="^leven/(.*)$" /><conditions><add input="{HTTP_HOST}" pattern="^localhost$" /></conditions><action type="Rewrite" url="https://leven.com.cn/{R:1}" /></rule>
      </rules>
      <outboundRules>
        <rule name="Add application prefix"><match filterByTags="A,Img,Script,Link" pattern="^/(.*)$" /><conditions><add input="{URL}" pattern="^/leven/.*" /></conditions><action type="Rewrite" value="/leven/{R:1}" /></rule>
      </outboundRules>
    </rewrite>
  </system.webServer>
</configuration>

進階提示

  • SSL 卸載:前端 IIS 綁定 443 + 憑證,後端 HTTP,ARR 自動處理 X-Forwarded-Proto。
  • 健康檢查:ARR 伺服器陣列 → Health Test URL、Interval、Timeout,自動摘除故障節點。
  • 快取:ARR Cache 可快取靜態資源,減輕後端壓力。
  • 負載均衡:將多台後端伺服器加入同一 Server Farm,演算法:Weighted Round Robin / Least Requests / IP Hash。
copyright © 萬盛學電腦網 all rights reserved