最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - IIS Reverse Proxy with ARR & URL Rewrite Only Works for Root Route (), Other Routes Fail - Stack Overflow

programmeradmin2浏览0评论

I am using IIS as a reverse proxy to forward requests to my Node.js API running on localhost (port 3000). However, only the root route (ppas_app/) works, and any other endpoint like ppas_app/api/test or ppas_app/api/users returns 404 Not Found.

•   IIS as Reverse Proxy
•   Node.js API running on port 3000
•   ARR (Application Request Routing) and URL Rewrite enabled
•   IIS is configured to forward all requests to http://localhost:3000

Issue Details:

  1. Accessing ✅ Works as expected
  2. Accessing ❌ Returns 404

Not Found 3. Accessing http://localhost:3000/api/test ✅ Works directly on Node.js 4. No errors in IIS logs, but the API routes don’t seem to be forwarded properly.

**my web.config : **

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <system.webServer>
      <handlers>
        <add name="httpPlatformHandler" path="*" verb="*"       modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
      </handlers>
      <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log"   startupTimeLimit="20" processPath="D:\api\node.exe" arguments=".\app.js">
        <environmentVariables>
          <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
          <environmentVariable name="NODE_ENV" value="Production" />
        </environmentVariables>
      </httpPlatform>
      <rewrite>
      <rules>
        <rule name="ReverseProxyInboundRule1" stopProcessing="true">
          <match url="(.*)" />
          <action type="Rewrite" url="http://localhost:3000/{R:1}"    />
        </rule>
      </rules>
      <outboundRules>
        <rule name="ReverseProxyOutboundRule1"    preCondition="ResponseIsHtml1">
          <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:3000/(.*)" />
          <action type="Rewrite" value="http{R:1}://"www.example"/{R:2}" />
        </rule>
        <preConditions>
          <preCondition name="ResponseIsHtml1">
              <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>
  </system.webServer>
</configuration>

Node.js Api sample test:

const port = process.env.PORT || 3000;
    
app.get('/', function(req, res){
    return res.status(200).send('First');
})
    
app.get('/api/test', function(req, res){
    return res.status(200).send('First Server');
})
    
app.get('/nodetest/api/test', function(req, res){
    return res.status(200).send('as an IIS application');
})
    
app.listen(port, () =>{
    console.log(`Server is listening to ${port}.....`);
})

I am using IIS as a reverse proxy to forward requests to my Node.js API running on localhost (port 3000). However, only the root route (ppas_app/) works, and any other endpoint like ppas_app/api/test or ppas_app/api/users returns 404 Not Found.

•   IIS as Reverse Proxy
•   Node.js API running on port 3000
•   ARR (Application Request Routing) and URL Rewrite enabled
•   IIS is configured to forward all requests to http://localhost:3000

Issue Details:

  1. Accessing http://www.example/spintas_app ✅ Works as expected
  2. Accessing http://www.example/spintas_app/api/test ❌ Returns 404

Not Found 3. Accessing http://localhost:3000/api/test ✅ Works directly on Node.js 4. No errors in IIS logs, but the API routes don’t seem to be forwarded properly.

**my web.config : **

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <system.webServer>
      <handlers>
        <add name="httpPlatformHandler" path="*" verb="*"       modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
      </handlers>
      <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log"   startupTimeLimit="20" processPath="D:\api\node.exe" arguments=".\app.js">
        <environmentVariables>
          <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
          <environmentVariable name="NODE_ENV" value="Production" />
        </environmentVariables>
      </httpPlatform>
      <rewrite>
      <rules>
        <rule name="ReverseProxyInboundRule1" stopProcessing="true">
          <match url="(.*)" />
          <action type="Rewrite" url="http://localhost:3000/{R:1}"    />
        </rule>
      </rules>
      <outboundRules>
        <rule name="ReverseProxyOutboundRule1"    preCondition="ResponseIsHtml1">
          <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:3000/(.*)" />
          <action type="Rewrite" value="http{R:1}://"www.example"/{R:2}" />
        </rule>
        <preConditions>
          <preCondition name="ResponseIsHtml1">
              <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>
  </system.webServer>
</configuration>

Node.js Api sample test:

const port = process.env.PORT || 3000;
    
app.get('/', function(req, res){
    return res.status(200).send('First');
})
    
app.get('/api/test', function(req, res){
    return res.status(200).send('First Server');
})
    
app.get('/nodetest/api/test', function(req, res){
    return res.status(200).send('as an IIS application');
})
    
app.listen(port, () =>{
    console.log(`Server is listening to ${port}.....`);
})
Share Improve this question edited Mar 17 at 10:11 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 15 at 9:13 Syahrul IzwanSyahrul Izwan 1 1
  • i have question if you are using the http handle with iis to serve the application of node js from iis may i know why you have set the reverse proxy ? is the hosted app is different from you have set the reverse proxy – Jalpa Panchal Commented Mar 17 at 2:36
Add a comment  | 

1 Answer 1

Reset to default 0

You could try to add the base path in your node js. as your node js path is design to handle the request with the /api/test or /api/users but right now your requesting with the ppas_app/api/users which node js does not understnd.

or else you can set iis url rewrite rule to only fowrad the request of the sub application to the node js app something like below:

Insted of:

<match url="(.*)" />

use:

<match url="^ppas_app/(.*)" />

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论