Serving static files from iisnode with URL Rewrite

谁说我不能喝 提交于 2019-12-02 00:35:30

The best way to serve static content in iisnode is to configure the URL rewriting module such that the IIS static file handler handles requests for static content rather than node.js. Having IIS serve static content has a large performance benefit over serving these files using any node.js mechanisms due to kernel level optimizations around caching and just not having to break into JavaScript code.

For a boilerplate web.config configuration that achieves this see https://github.com/tjanczuk/iisnode/issues/160#issuecomment-5606547

I had some issues using the suggested rule config, so I made some changes:

<system.webServer>
  <handlers>
    <clear />
    <add name="iisnode" path="/index.js" verb="*" modules="iisnode" />
    <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
  </handlers>
  <rewrite>
    <rules>
      <rule name="static">
        <action type="Rewrite" url="www{REQUEST_URI}" />
      </rule>
      <rule name="serve-static" stopProcessing="true">
        <conditions logicalGrouping="MatchAny">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          <add input="{REQUEST_URI}" pattern="^/www/$" />
        </conditions>
      </rule>
      <rule name="node">
        <action type="Rewrite" url="index.js" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
  • First rule prefixes all requests with www, my client directory.
  • Second rule stops processing if the file exists or if the root path is requested
  • Third rule rewrites anything else to the name of my backend Node script so that it can be picked up by the iisnode handler.

Note the iisnode handler path is set to /index.js which seems to eliminate conflicts with client files of the same name.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!