14

How to Enable Application Request Routing on Azure App Service

 3 years ago
source link: https://edi.wang/post/2020/11/2/how-to-enable-application-request-routing-on-azure-app-service
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

We typically use Application Request Routing (ARR) module to host a reverse proxy on IIS. Azure App Service is also using IIS as it's gateway, which should work for reverse proxy, but it won't work by default. Let's see how can we enable ARR on Azure App Service.

The Experiment


I have https://dropdatabase.run/ domain, and I want to reverse proxy https://996.icu/ under https://dropdatabase.run/996. So I created a web.config file under wwwroot folder.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="996" stopProcessing="false">
          <match url="996(.*)" />
          <action type="Rewrite" url="https://996.icu/{R:1}" appendQueryString="true" logRewrittenUrl="false" />
        </rule>
		<rule name="996js" stopProcessing="false">
          <match url="js/(.*)" />
          <action type="Rewrite" url="https://996.icu/js/{R:1}" appendQueryString="true" logRewrittenUrl="false" />
        </rule>
      </rules>
    </rewrite>
    <httpProtocol>
     <customHeaders>
        <add name="strict-transport-security" value="max-age=15552001; includeSubDomains; preload" />
     </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

It works on my local machine, but on Azure App Service, it went 404.

img-9d6259b3-0c6b-4a6a-9b2b-545e94bc2556.png

The web.config is correct, but it won't work because Azure App Service doesn't by default enable the ARR module although it is preinstalled.

The Solution


Let's do some magic that you won't easily find on Microsoft document. To enable ARR on Azure App Service, you need to create an xdt file and upload to your site directory.

Go to Advanced Tools on your website's management blade.

img-8adf2a9b-ec2e-4c4e-965d-03abf6ffa194.png

Go to Debug console, CMD or PowerShell

img-87fb6167-4a06-458a-8392-4728ad1ab2e0.png

Enter site directory

img-933fad70-6f13-48a7-a7e1-ddb4a46d68cb.png

Click + New file

Enter the magic filename applicationHost.xdt and click the pen icon for editing it's content

img-557da156-ffe0-4601-bd14-577bfce234b1.png

Copy the following content and save the file

<?xml version="1.0" encoding="UTF-8"?>
<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>

Go back to Azure App Service and restart your website

The ARR will be enabled after the website restarts, and our reverse proxy works as expected now

img-5637cf44-ec99-47e4-a48f-a1725195d4af.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK