0

I have a website with the following structure at the root for a Node.js app

package.json
server.js
public
    index.html
    css
    js
    images
...other folders

I would like to take the person only inside the public folder with a forced HTTPS connection.

My current web.config file looks like this

<?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^server.js\/debug[\/]?" />
        </rule>

        <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

        <!-- All other URLs are mapped to the node.js site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="server.js"/>
        </rule>

         <rule name="Force HTTPS" enabled="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>    

        <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^zupbot\.azurewebsites\.net$" />
            </conditions>
            <action type="Redirect" url="https://www.zup.chat/{R:0}" />  
          </rule>


      </rules>
    </rewrite>

    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled

      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <!--<iisnode watchedFiles="web.config;*.js"/>-->
  </system.webServer>
</configuration>

Whenever I try to access my website, it tries finding a server.js and gives me this error {"code":"NotAuthorized","message":"/server.js"}

My node.js code does this to serve static files

server.get('/.*/', restify.serveStatic({

    //Ensure that people can only access the files within the public directory and none of the protected server files
    directory: __dirname + '/public',
    default: constants.INDEX_HTML,
    match: /^((?!server.js).)*$/   // we should deny access to the application source
}));

How can I take the person straight inside the public folder always with an HTTPS connection? Thank you for your help in advance.

UPDATE 1

I added the HTTPS redirect before any other rule that has a stopProcessing=true and it works , however if I go to my native site http://.azurewebsites.net, it still takes me to the https version of it, how can I redirect sitename.azurewebsites.net to the https version of my custom domain?

PirateApp
  • 5,433
  • 4
  • 57
  • 90

1 Answers1

1

Please try the following rewrite rules in web.config:

<rule name="DynamicContent" stopProcessing="true">
     <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
     </conditions>
     <action type="Rewrite" url="server.js"/>
</rule>

<rule name="Redirect to https" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
  <match url="(.*)"/>
  <conditions>
      <add input="{HTTPS}" pattern="Off"/>
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" />
</rule>

Which will rewrite the url your_site.azurewebsites.net/<asset> to https protocol if there is a file named <asset> in public folder as you configured in restify.serveStatic.

And it will not rewrite the url those can match the route setting in your restify application.

Any further concern, please feel free to let me know.

Gary Liu
  • 13,758
  • 1
  • 17
  • 32