TransWikia.com

Microsoft IIS Rules with Reverse Proxy and Static

Server Fault Asked by Dean Christian Armada on December 23, 2021

I have an IIS Server that originally hosts static HTML in wwwroot but then we decided that if a Baidu Spider tries to crawl we will map the traffic to our NuxtJS Web Server running also beside the IIS Server.

So we installed ARR (Application Request Routing) to enable the reverse proxy it is working fine and now we tried to test first where in URL Rewrite Inbound Rule:

  • If Baidu Spider map the request to the NuxtJS Server (http://localhost:3000) (This is for testing first)

So with that we expect that if common users requested then it will serve the static HTML in wwwroot instead since it did not hit a rule but instead I got a 500.

TAKE NOTE:

  • I am using Windows 2012 and IIS version 8.5
  • if I disable my Inbound Rule then it will sure serve the static HTML Files

So do I need to create another rule? I was expecting that if it did not hit any rule the default behaviour for which it just reads in wwwroot will happen

UPDATE

  • I decided to create another rule for my static HTML serving. Below is the generated rewrite rule by IIS. My problem now is that the second rule does not work. When I disable the first rule the second rule then works
<?xml version="1.0" encoding="UTF-8"?>
<rules>
    <clear />
    <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <action type="Rewrite" url="http://localhost:3000/{R:1}" />
        <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="^((?Baidu).)*$" />
        </conditions>
    </rule>
    <rule name="StaticHTMLForBaiduCrawler" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="^((?!Baidu).)*$" />
        </conditions>
        <action type="Rewrite" url="{R:1}/index.html" />
    </rule>
</rules>

One Answer

The regular expression in the condition in your first rule is not a valid regex for IIS. If you want the first rule to be hit when the user agent contains Baidu and the second when it does not then you can just do something like below. Note the negate in the condition in the second rule:

<?xml version="1.0" encoding="UTF-8"?>
<rules>
    <clear />
    <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <action type="Rewrite" url="http://localhost:3000/{R:1}" />
        <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="Baidu" />
        </conditions>
    </rule>
    <rule name="StaticHTMLForBaiduCrawler" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_USER_AGENT}" pattern="Baidu" negate="true" />
        </conditions>
        <action type="Rewrite" url="{R:1}/index.html" />
    </rule>
</rules>

Answered by Jeroen on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP