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

Node.js+IIS子应用部署遇404错误求助:独立站点运行正常

问题描述

我用Node.js+IIS开发网站,内部IP环境运行正常。现在想通过已有公网IP,在Default Web Site中添加TS_AS子应用实现外部访问,但部署后访问该子应用出现404错误。将相同代码部署到独立的test站点(端口8080)时可正常显示,修改index.js后问题仍未解决。

相关配置代码

index.js

const express = require('express');
const { user } = require('./dbconfig');
const app = express() ;
const PORT = process.env.PORT || 80;
const pool = require('./sql');
const path = require('path');


app.set('view engine','ejs');
app.engine('html',require('ejs').renderFile);
app.set('views',path.join(__dirname,'/views'));
app.use(express.urlencoded({ extended: true}));
app.use(express.static(path.join(__dirname,'uploads')))
app.use(express.static(path.join(__dirname,'/views')))


app.get('/',async(req,res) =>{
res.send('just do it');
});

app.listen(PORT, () => {
    console.log(`Server on = http://localhost:${PORT}/`);
});

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers accessPolicy="Read, Script">
            <add name="iisnode" path="index.js" verb="*" modules="iisnode" requireAccess="Script" />
        </handlers>
        <iisnode watchedFiles=".js;node_modules\*;routes\*.js;views\*.jade" nodeProcessCommandLine="C:\program Files\nodejs\node.exe" />
        <rewrite>
            <rules>
                <rule name="SendToNode" enabled="true">
                    <match url="/*" />
                    <action type="Rewrite" url="index.js" />
                </rule>
            </rules>
        </rewrite>
        <httpRedirect enabled="false" destination="" />
        <staticContent>
            <mimeMap fileExtension=".ejs" mimeType="text/html" />
        </staticContent>
    </system.webServer>
</configuration>

applicationHost.config 相关配置

<sites>
            <site name="Default Web Site" id="1" serverAutoStart="true">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
                </application>
                <application path="/--------" applicationPool="DefaultAppPool">
                    <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\--------" />
                </application>
                <application path="/TS_AS" applicationPool="DefaultAppPool">
                    <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\TS_AS" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:80:" />
                </bindings>
            </site>
            <site name="test" id="3" serverAutoStart="true">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\TS_AS" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:8080:" />
                </bindings>
            </site>
        </sites>
解决方案

1. 修正URL重写规则

作为子应用部署时,IIS重写规则需适配/TS_AS前缀,原规则未处理子应用上下文路径,导致请求无法正确转发到index.js。修改web.config中的重写规则:

<rewrite>
    <rules>
        <rule name="TS_AS_Rewrite" enabled="true">
            <match url="^TS_AS/(.*)" />
            <action type="Rewrite" url="TS_AS/index.js/{R:1}" />
        </rule>
    </rules>
</rewrite>

2. 调整Express路由前缀

Express默认路由基于根路径/,需适配子应用的/TS_AS访问路径,有两种修改方式:

  • 直接给路由添加前缀:
app.get('/TS_AS',async(req,res) =>{
res.send('just do it');
});
  • 使用路由统一添加前缀:
const router = express.Router();
router.get('/',async(req,res) =>{
res.send('just do it');
});
app.use('/TS_AS', router);

3. 检查IIS子应用配置

  • 确认TS_AS子应用的应用池(DefaultAppPool)对C:\inetpub\wwwroot\TS_AS目录有读写权限,给该目录添加IIS_IUSRS用户的权限。
  • 检查子应用是否启用了iisnode模块,确保模块已安装并在子应用中激活。
  • 执行iisreset命令重启IIS,使配置生效。

4. 验证端口与网络配置

确认Default Web Site的80端口未被占用,同时公网IP的80端口已在防火墙/安全组中开放,允许外部访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 18:41:19