Issue with outbound rule not rewriting correctlyRSS

1 reply

Last post Jun 30, 2020 03:08 AM by Jalpa Panchal

‹ Previous Thread|Next Thread ›
  • Issue with outbound rule not rewriting correctly

    Jun 29, 2020 04:01 PM|deanwsmith|LINK

    I have Netcam Studio installed on a machine running IIS. The machine has a domain that can be accessed via the internet. I have successfully forwarded port 8100 on my router so that I can access NCS from the internet. However, this does not use https, and I want to use the URL Rewrite functionality of IIS to create a reverse proxy from my domain name to the NCS server.

    Here are my steps to configure this in IIS:

    1. Install Application Request Routing
    2. Create an application called “ncs” under the Default web site
    3. Using URL Rewrite, create a set of rules to forward the requests and rewrite the responses

    Here are the rules from the generated web.config files:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <remove name="X-Powered-By" />
                </customHeaders>
            </httpProtocol>
            <rewrite>
                <rules>
                    <clear />
                    <rule name="ReverseProxyInboundRuleNetcamStudio" stopProcessing="true">
                        <match url="ncs(/)?(.*)" />
                        <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                        <action type="Rewrite" url="http://localhost:8100/{R:2}" />
                        <serverVariables>
                            <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                            <set name="HTTP_ACCEPT_ENCODING" value="" />
                        </serverVariables>
                    </rule>
    			</rules>
                <outboundRules>
                    <rule name="ReverseProxyOutboundRuleNetcamStudio" preCondition="ResponseIsHtml1">
                        <match filterByTags="A, Form, Frame, IFrame, Img, Input, Link, Script" pattern="^http(s)?://localhost:8100/(.*)" />
                        <action type="Rewrite" value="http{R:1}://[my domain]/ncs/{R:2}" />
                    </rule>
                </outboundRules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    When I point my browser to https://[my domain]/ncs/ I get a blank page. When I view the source I see the following:

    <!DOCTYPE html>
    <html>
      <head>
        <base href="/"/>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Netcam Studio</title>
        <link rel="icon" type="image/x-icon" href="favicon.ico" />
    
        <!--</script>-->
        <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script> -->
    
        <!-- D3 and customized EventDrops -->
        <script src="./assets/lib/d3/d3.js"></script>
        <!--<script src="https://d3js.org/d3.v4.js"></script>-->
        <script src="./assets/lib/event-drops/dist/eventDrops.js"></script>
    
        <!-- Video.JS -->
        <link href="./assets/lib/video-js/video-js.min.css" rel="stylesheet" />
        <script src="./assets/lib/video-js/video.js"></script>
    
        <!-- Chart.JS -->
        <script src="./assets/lib/chart.js/dist/Chart.js"></script>
        <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.min.js"></script>-->
    
        <!-- SoundManager 2 -->
        <script src="./assets/lib/soundmanager2/soundmanager2-nodebug-jsmin.js"></script>
    
        <!-- PrimeNG -->
        <!--
      <link rel="stylesheet" type="text/css" href="./lib/primeng/themes/omega/theme.css" />
      <link rel="stylesheet" type="text/css" href="./lib/primeng/primeng.min.css" />
      <link rel="stylesheet" type="text/css" href="./lib/font-awesome-4.7.0/css/font-awesome.css" />
      -->
    
        <!-- momentJS-->
        <script src="./assets/lib/moment-with-locales.min.js"></script>
      <link rel="stylesheet" href="styles.01ecddb3c489cc50e314.css"></head>
      <body>
        <app-root>
          <div class="loading"></div>
        </app-root>
      <script src="runtime-es2015.e8a2810b3b08d6a1b6aa.js" type="module"></script><script src="runtime-es5.e8a2810b3b08d6a1b6aa.js" nomodule defer></script><script src="polyfills-es5.1902a4db008bc3386943.js" nomodule defer></script><script src="polyfills-es2015.3c23419c5698e054ace2.js" type="module"></script><script src="main-es2015.87c5ee2239c550eb5fce.js" type="module"></script><script src="main-es5.87c5ee2239c550eb5fce.js" nomodule defer></script></body>
    </html>
    

    This means it should be working - but nothing is being displayed. I suspect the outbound rewrite isn’t quite correct, as it seems to be resolving the scripts to:

    https://[my domain//assets/lib/moment-with-locales.min.js

    Rather than

    https://[my domain]/ncs/assets/lib/moment-with-locales.min.js

    Does anyone have any ideas on how to resolve this?
    Dean

  • Re: Issue with outbound rule not rewriting correctly

    Jun 30, 2020 03:08 AM|Jalpa Panchal|LINK

    Hi,

    First try to remove the <base href="/"/> from your page. 

    try to use below URL rewrite rule:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <remove name="X-Powered-By" />
                </customHeaders>
            </httpProtocol>
            <rewrite>
                <rules>
                    <clear />
                    <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                        <match url="(.*)" />
                        <action type="Rewrite" url="http://localhost:8100/{R:1}" />
                    </rule>
    			</rules>
                <outboundRules>
                    <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
                        <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:8100/(.*)" />
                        <action type="Rewrite" value="http{R:1}://localhost/ncs{R:2}" />
                    </rule>
                    
                    <preConditions>
                        <preCondition name="ResponseIsHtml1">
                            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                        </preCondition>
                    </preConditions>
                </outboundRules>
            </rewrite>
            <tracing>
                <traceFailedRequests>
                    <add path="*">
                        <traceAreas>
                            <add provider="WWW Server" areas="Rewrite" verbosity="Verbose" />
                        </traceAreas>
                        <failureDefinitions statusCodes="100-900" />
                    </add>
                </traceFailedRequests>
            </tracing>
        </system.webServer>
    </configuration>

    add this rule in the application web.confifg file, not at the default site level.

    and make sure you enabled reverse proxy in iis arr.

    if you still face and issue you could try to run a failed requests tracing in iis and check what is going wrong.

    https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
‹ Previous Thread|Next Thread ›