1. 程式人生 > >【應用服務 App Service】 App Service Rewrite 例項 - 反向代理轉發功能

【應用服務 App Service】 App Service Rewrite 例項 - 反向代理轉發功能

問題描述

在使用Azure App Service(應用服務)時,有時候需要在不同的站點之間進行跳轉,但是希望通過通過訪問同一個域名的方式來實現反向代理。如果建立應用時候選擇的是Window服務,這時候可以參考以下的方式配置IIS 的Proxy + Rewrie

如網站一為PHP站點,它的首頁是:https://lbphptest.chinacloudsites.cn/, 網站二是Java站點,它的首頁是:https://lbjavatest.chinacloudsites.cn/

站點一:PHP

站點二:JAVA

在使用反向代理後,在站點一種配置Proxy代理後,請求就會發送到Java站點,但是URL顯示的依舊為PHP站點地址。

設定步驟

 

一: 準備好兩個App Service站點

二:在需要做方向代理的站點種新增applicationHost.xdt和web.config 檔案,如以上的例子中使用的是PHP站點作為代理站點。

  • 新增ApplicationHost.xdt檔案,開啟Proxy enable屬性為true。檔案需要新增在D:\home\site下,與wwwroot目錄同級

ApplicationHost.xdt的內容為:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" />
    </system.webServer>
</configuration>
  • 新增web.config檔案,設定rewrite的規則。

web.config的內容為(高亮部分即為需要配置的跳轉站點):

<configuration>  
<system.webServer>  
<rewrite>  
<rules>  
<rule name="Proxy" stopProcessing="true">  
<match url="^proxy/?(.*)" />  
<action type="Rewrite" url="http://lbjavatest.chinacloudsites.cn/{R:1}" />  
</rule>  
</rules>  
</rewrite>  
</system.webServer>  
</configuration>

注:完成以上步驟後,需要重啟PHP站點。

 

參考資料

Web 應用如何實現反向代理轉發功能:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-howto-realize-reverse-proxy-and-forward-function

 

URL Rewrite Module Configuration Reference:https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#redirect-action

 Rewrite Rules Overview

A rewrite rule defines the logic of what to compare or match the request URL with, and what to do if the comparison is successful.

Rewrite rules consists of the following parts:

  • Pattern – The rule pattern is used to specify either the regular expression or a wildcard pattern that is used to match URL strings.
  • Conditions – The optional conditions collection is used to specify additional logical operations to perform if a URL string matches the rule pattern. Within the conditions, you can check for certain values of HTTP headers or server variables, or verify if the requested URL corresponds to a file or directory on a physical file system.
  • Action – The action is used to specify what to do if the URL string matches the rule pattern and all the rule conditions are met.

 

附加一: App Service中啟動jar包的web.config中加入rewrite規則

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
 
    <!-- BEGIN rule TAG FOR HTTPS REDIRECT -->
    <rewrite>
      <rules>
        <rule name="Force HTTPS" enabled="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                  <add input="{HTTP_USER_AGENT}" pattern="Initialization" ignoreCase="true" negate="true" />
                  <add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" ignoreCase="true" negate="true" />
                  <add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    <!-- END rule TAG FOR HTTPS REDIRECT -->
 
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <!-- https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference -->
    <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
         arguments="-Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8 -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\what.jar&quot;"
         startupTimeLimit="300"
         startupRetryCount="10"
         requestTimeout="00:05:00"
         >
    </httpPlatform>
  </system.webServer>
</configuration> 

&n