【问题标题】:NodeJS at Azure : systematic 404 error on resource filesAzure 上的 NodeJS:资源文件出现系统性 404 错误
【发布时间】:2019-08-01 23:07:16
【问题描述】:

我们在 Azure 有一个 nodeJS webapp,它运行良好。

在流式传输服务器日志时,每当我加载页面时,我都会在所有资源(图像、css 等)上收到一堆 404 错误。但是页面显示正常。

详细错误显示如下:

Requested URL      https://[myappname]:80/settings.png
Physical Path      D:\home\site\wwwroot\settings.png
Logon Method       Anonymous
Logon User     Anonymous

请求的 URL 显然是错误的,它应该是 https://[myappname].azurewebsites.net/settings.png,这是给定资源的公共 URL,并且工作正常。 此问题会加载大量日志,导致目前无法使用 Web Server 日志。

谢谢!

编辑:与this problem 不同,我的页面加载正常,资源文件可用。

已解决我已将以下处理程序添加到我的 web.config 中:

<add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition="" />

【问题讨论】:

标签: node.js azure resources http-status-code-404


【解决方案1】:

我认为您需要在 web.config 中为静态文件内容配置规则集。

 <rule name="StaticContent">
                         <action type="Rewrite" url="public{REQUEST_URI}"/>
                    </rule>

在 Azure Web 应用程序上运行的 Node.js 应用程序通过 IISNode 托管在 IIS 上。因此,需要一个 web.config 文件来在 IIS 上配置您的应用程序。如果通过Continuous Deployment 将应用部署到 Azure 应用服务,Azure 将自动生成 web.config 文件。或者您可以从这里下载文件。

我发布默认 web.config 仅供参考”

<?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 app.js file is a node.js site to be handled by the iisnode module -->
               <add name="iisnode" path="app.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="^app.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="app.js"/>
                    </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

               To debug your node.js application:
                 * set the debuggingEnabled option to "true"
                 * enable web sockets from the portal at https://manage.windowsazure.com/#Workspaces/WebsiteExtension/Website/aarontestnode/configure
                 * browse to https://aarontestnode.azurewebsites.net/app.js/debug/

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

希望对你有帮助。

【讨论】:

  • 一步一步调查.. 当我简单地评论整个 StaticContent Rewrite 规则时,我发现问题仍然存在。基本上任何资源(css,png等)上的404,但网页正常显示。我的 web.config 是标准配置,但我不知道是谁将呼叫转发到那些无效的 URL
【解决方案2】:

感谢您的回答。

我有几乎相同的 web.config,自动生成。您指出的规则略有不同:

    <rule name="StaticContent">
      <action type="Rewrite" url="public{PATH_INFO}"/>
    </rule>

我的服务器代码包括:

app.use(express.static(path.join(__dirname, 'public')));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多