You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS在IIS反向代理下路由无法访问,3000端口正常

IIS反向代理下Node.js应用路由无法访问的问题排查

我有一个运行在3000端口的Node.js应用,配置了IIS反向代理(端口80)对外提供服务。直接访问http://localhost:3000时所有路由正常,但通过IIS访问http://localhost时,仅根路径的静态页面和/api-docs的Swagger-UI可正常加载,其余路由均返回Cannot GET错误。

初始配置信息

IIS反向代理Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:3000/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

应用结构

node_modules
routes
    api1
    api2
server.js

server.js核心代码

// MODULES AND REQUIRES
const express = require("express");
const app = express();
const path = require('path');
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const objectMapper = require('object-mapper');
const cors = require('cors');

// Require Routes
var api1 = require('./routes/api1.js')
var api2 = require('./routes/api2.js')

// PORTS AND BASIC EXPRESS APP SETTINGS
const port = process.env.PORT || 3000;

// CORS CONFIG
app.use(cors({
  origin: '*'
}));

// Ignore Favicon
const ignoreFavicon = (req, res, next) => {
  if (req.originalUrl.includes('favicon.ico')) {
      res.status(204).end();
  }
  next();
};
app.use(ignoreFavicon);

// Root Route
app.get('/', (req, res) => {
      res.sendFile(path.join(__dirname, '/public/client.html'));
});

// SWAGGER CONFIG
const options = {
  customCss: '.swagger-ui .topbar { display: none } .swagger-ui .scheme-container { display: none }'
};

const swaggerOptions = {
  swaggerDefinition: {
    info: {
      version: "2.0.0",
      title: "My App",
      description: "API documentation",
      contact: {
        name: "My Name"
      },
      servers: [{"url":"http://localhost:3000", "description": "Development server"}]
    }
  },
  apis: ["./routes/*.js"],
};

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs, options));

// API Routes
app.use('/api1', api1)
app.use('/api2', api2)  

// Start Server
app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

后续更新与排查

更新1:尝试HTTP Platform Handler

安装HTTP Platform Handler后配置如下,但问题依旧:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httppPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe" arguments=".\server.js">
            <environmentVariables>
                <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
                <environmentVariable name="NODE_ENV" value="Production" />
            </environmentVariables>
        </httpPlatform>
  </system.webServer>
</configuration>

测试了iisnode、ARR反向代理、HTTP Platform Handler三种方式,均出现相同路由错误,怀疑Node应用未正常运行,或D盘目录存在权限问题。

更新2:端口问题排查

查看日志发现,Node应用并未使用指定的3000端口,而是随机选择端口,这可能是路由无法访问的核心原因。


问题根源与解决步骤

1. 端口环境变量冲突

当使用HTTP Platform Handler时,配置中通过%HTTP_PLATFORM_PORT%设置了PORT环境变量,会覆盖代码中process.env.PORT || 3000的默认端口,导致Node应用使用随机端口运行,反向代理目标3000端口无服务,路由无法转发。

  • 若坚持用反向代理模式:删除HTTP Platform Handler配置中的PORT环境变量,确保Node应用固定在3000端口运行。
  • 若用HTTP Platform Handler模式:修改反向代理目标端口为%HTTP_PLATFORM_PORT%,或直接通过Handler托管应用,无需额外反向代理。

2. IIS权限配置

应用位于D盘,需确保IIS应用池身份拥有D盘目录的读取、执行权限:

  • 右键应用池 → 高级设置 → 进程模型 → 标识,设置为LocalSystem或具备D盘权限的自定义用户。

3. 反向代理规则优化

若使用ARR反向代理,需确保规则完整传递请求参数,可添加服务器变量配置:

<rule name="ReverseProxyInboundRule1" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="http://localhost:3000/{R:1}" />
    <serverVariables>
        <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
        <set name="HTTP_ACCEPT_ENCODING" value="" />
    </serverVariables>
</rule>

同时确认IIS的URL重写模块和ARR模块已正确安装启用。

4. 应用日志排查

在Node路由中添加请求日志,确认应用是否接收到转发的请求:

app.use('/api1', (req, res, next) => {
    console.log(`Received request: ${req.method} ${req.originalUrl}`);
    next();
}, api1);

查看HTTP Platform Handler生成的node.log文件,确认应用启动是否有报错。

内容的提问来源于stack exchange,提问作者Kode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 12:50:55