使用IIS實現反向代理

IIS的反向代理是經過ARR模塊來完成的,ARR模塊須要另外安裝,並且只能經過Web PlatForm Installer安裝。關於安裝來源與步驟,帖子已有不少,不作描述。啓用「Application Request Routing」功能以及簡單設置URL Rewrite的步驟也不作描述,帖子不少,幾乎是千篇一概的貼圖,意義不大。在此只記錄一些知識要點:html

一、規則設置中的第一個步驟「匹配URL」,匹配來源僅僅是請求的完整URL中除去協議、主機頭、端口號、querystring後剩下的那一部分:正則表達式

 

好比,預計瀏覽器的請求地址爲:http://szh-it-2e02d/test/KWF/Preview.html?ticket=b037ec8c-6579-4e27-be43-dfc3e821c2d2&flowID=1278355,那麼僅僅是」test/KWF/Preview.html「這一段會用於匹配測試。所產生的捕獲組「{R:0}」「{R:1}」,即爲正則表達式執行時匹配成功的塊,數目因正則的不一樣而不一樣,將做爲變量,可在後面的配置中引用。瀏覽器

 

二、「條件」這一塊設置中,條件輸入必須是可用的變量,而可使用的變量有哪些呢?微軟的幫助文檔url-rewrite-module-configuration-reference一文已經列舉得很清楚:服務器

  For an HTTP URL in this form: http(s)://<host>:<port>/<path>?<querystring>測試

  • The <path> is matched against the pattern of the rule.
  • The <querystring> is available in the server variable called QUERY_STRING and can be accessed by using a condition within a rule.
  • The <host> is available in the server variable HTTP_HOST and can be accessed by using a condition within a rule.
  • The <port> is available in the server variable SERVER_PORT and can be accessed by using a condition within a rule.
  • Server variables SERVER_PORT_SECURE and HTTPS can be used to determine if a secure connection was used. These server variables can be accessed by using a condition within a rule.
  • The server variable REQUEST_URI can be used to access the entire requested URL path, including the query string.

 每一條規則,能夠設置零到若干個條件,只有知足步驟1中的URL模式匹配時,纔會進入步驟2的「條件」驗證,只有知足條件驗證,纔會進入後續的處理。若是設置了條件,那麼也會產生一些變量值,形如{C:0}{C:1},可在後續中使用。this

  界面上的條件設置只有按正則匹配這一種方式,實際上還有另外兩種,但不支持可視化操做,須要手工編寫配置文件。url

  • IsFile – This match type is used to determine whether the input string contains a physical path to a file on a file system. If a condition input string is not specified, the URL Rewrite Module uses the physical path of the requested file as a default value for the condition input. This match type can be used only for distributed rules.
  • IsDirectory – This match type is used to determine whether the input string contains a physical path to a directory on a file system. If a condition input string is not specified, the URL Rewrite Module uses the physical path of the requested file as a default value for the condition input. This match type can be used only for distributed rules.

 

三、服務器變量。這一步能夠自由添加一些變量值,也能夠修改變量值,如:指望把端口從80轉到81,能夠作以下配置:3d

 

四、操做。這一步驟裏首先要選擇操做類型,若是是要作反向代理,那麼應該選擇「重寫」這一種類型。重寫的URL中,可使用變量、常量來描述,好比:代理

 

五、很重要也是頗有用的一點,使用反向代理時,客戶端(瀏覽器)只須要可以訪問反向代理服務器便可,與代理後的目標服務器是不產生直接訪問的,代理充當了一個請求轉發的做用。這一點已作驗證。orm

相關文章
相關標籤/搜索