IIS returns 500 when Node app returns a 4xx

2019-04-10 04:38发布

问题:

I have also asked this on the iisnode github project

I am using IISNode via a Windows Azure Website.

If my Node app returns a 2xx status code (200, 201, etc), then all is well and works as expected.

if my Node app returns a 4xx status code, for example: response.send(404, "not found") (I am using Restify) then I get a 500 sent to the client and the body is simply "The page cannot be displayed because an internal server error has occurred."

If I do azure site log tail <sitename>, then when the 500 gets sent to the client, the log contains HTML of a 404.

...
<body> 
<div id="content"> 
<div class="content-container"> 
<h3>HTTP Error 404.0 - Not Found</h3> 
<h4>The resource you are looking for has been removed, had its name changed,
or is temporarily unavailable.</h4> 
</div> 
...

I really just want IISNode/IIS to take my 404 and send it on down to the client. This is also true of 401s, 409s, etc. They all cause a 500 to get sent instead.

I have tried <customErrors mode="off" /> and <httpErrors existingResponse="passThrough" /> in my web.config to no avail. Here is my web.config now:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="off" />
  </system.web>
  <system.webServer>         
    <httpErrors existingResponse="PassThrough" />
    <staticContent>
       <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
       <mimeMap fileExtension=".ico" mimeType="image/x-icon" />
    </staticContent>
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="DynamicContent">
           <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
           </conditions>
           <action type="Rewrite" url="server.js"/>
           <match url="/*" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

For what it's worth, I can't get IISnode to do anything but return 200s from my node app. Trying to get IIS to serve my static files, enable node-inspector, or really any of the more interesting IISNode features cause a 500 for me. No idea why, and would love to get past it!

Similar StackOverflow Questions

this question is pretty much exactly my problem. Except that <httpErrors existingResponse="PassThrough" /> does not work for me. I feel like I have something more fundamentally wrong with my IISNode setup, so if anyone has any ideas on things to try I'm all ears.

回答1:

for some reason the following section in your config is causing this issue:

<staticContent>
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    <mimeMap fileExtension=".ico" mimeType="image/x-icon" />
</staticContent>

As a workaround, remove this section if possible and retry. I will investigate why this section is causing this issue.



回答2:

As Ranjith pointed out, the staticContent section is the cause of the conflict. I just found you can limit those entries inside of location entries, so you can get all of IISNode as desired and still serve up static content. My web.config now looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="off" />
  </system.web>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="Redirect to https" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" appendQueryString="true" />
        </rule>

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

        <rule name="DynamicContent">
           <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
           </conditions>
           <action type="Rewrite" url="server.js"/>
        </rule>
      </rules>
    </rewrite>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>

 <location path="public/fonts">
    <system.webServer>
      <staticContent>
         <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

The added location entry at the end works around the 500 and static content conflicts.



回答3:

I'm getting the exact same...500 error page returned from Azure/IIS. I used the web.config that Azure automatically generated as the basis, then added <customErrors mode="off" /> and <httpErrors existingResponse="PassThrough" />. I also removed the default staticContent node. Nothing is working.

Below is the web.config that I'm uploading to Azure. What in here is causing IIS/iisnode to return a 500 error page instead of the simple text error message I have node/express returning to the client?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="off" />
  </system.web>
  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
    <webSocket enabled="false" />
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^server.js\/debug[\/]?" />
        </rule>
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="server.js"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>


回答4:

This works for me:

<?xml version="1.0" encoding="utf-8"?> 
  <configuration>
    <system.webServer>
      <handlers>
        <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
      </handlers>
      <rewrite>
        <rules>
          <rule name="DynamicContent">
            <match url="/*" />
            <action type="Rewrite" url="app.js"/>
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
  </configuration>


回答5:

I had the same issue (IIS Node on Win Server 2012R2). So I did not get the target content in the body.

At the beginning I had response:

code: 400
body: Bad Request

Then I added <httpErrors existingResponse="PassThrough" /> and I got response:

code: 500
body: The page cannot be displayed because an internal server error has occurred

Then I have changed IIS Feature Delegation for "Error Pages" to "Read/Write" and this resolved my issue. Finally I had proper response:

code: 400
body: { "error": {...} }

I hope this helps.