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

如何在IIS上正确运行NextJS(13.4)?遇404.2错误求指导

Next.js 13.4 部署到 IIS 出现 404.2 错误的排查方案

ExpressJS应用在IIS上运行正常,但部署NextJS(13.4)时遇到404.2错误:
404.2错误截图

已参考相关部署指南配置,但问题仍存在。

项目配置情况

项目配置截图

入口文件 app.js 代码

const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = process.env.port || 5000
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
    createServer(async (req, res) => {
        try {
            const parsedUrl = parse(req.url, true)
            const { pathname, query } = parsedUrl

            if (pathname === '/a') {
                await app.render(req, res, '/a', query)
            } else if (pathname === '/b') {
                await app.render(req, res, '/b', query)
            } else {
                await handle(req, res, parsedUrl)
            }
        } catch (err) {
            console.error('Error occurred handling', req.url, err)
            res.statusCode = 500
            res.end('internal server error')
        }
    })
        .once('error', (err) => {
            console.error(err)
            process.exit(1)
        })
        .listen(port, () => {
            console.log(`> Ready on http://${hostname}:${port}`)
        })
})

web.config 配置

<configuration>
<system.webServer>
<handlers>
  <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>

<iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />

<rewrite>
  <rules>
    <rule name="nodejs">
      <match url="(.*)" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Rewrite" url="/app.js" />
    </rule>
  </rules>
</rewrite> 

<security>
  <requestFiltering>
    <hiddenSegments>
      <add segment="node_modules" />
      <add segment="iisnode" />
    </hiddenSegments>
  </requestFiltering>
</security>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>

package.json 脚本配置

"scripts": {
  "dev": "node app.js",
  "build": "next build",
  "start": "NODE_ENV=production node app.js",
  "lint": "next lint"
},

操作步骤

执行npm run build生成生产构建包,在IIS中创建基础配置的站点:
IIS站点配置截图

本地通过npm run dev可正常运行,但IIS访问时出现404.2错误。


解决方案

1. 确认iisnode模块状态

  • 打开IIS管理器,检查服务器级别的「模块」列表,确认iisnode已安装并启用;若未安装,重新下载安装官方iisnode模块。
  • 检查站点的「处理程序映射」,确认存在名称为iisnode、路径指向app.js的条目,若缺失手动添加。

2. 修正web.config路径与配置

  • 重写规则的action url改为相对路径:将<action type="Rewrite" url="/app.js" />修改为<action type="Rewrite" url="app.js" />(避免站点为虚拟目录时路径错误)。
  • 验证nodeProcessCommandLine中的node.exe路径是否正确,确保对应版本的Node.js已安装在该路径。
  • 开启iisnode日志排查:在<iisnode>节点添加日志配置,生成日志后查看具体错误:
    <iisnode nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" 
             loggingEnabled="true" 
             logDirectory="iisnode" />
    

3. 适配Windows环境的启动脚本

Windows环境下设置环境变量的语法与Linux不同,修改package.json的start脚本:

"start": "set NODE_ENV=production && node app.js"

4. 修正Next.js启动配置

  • 确保生产环境下dev参数为false:由于部署的是build后的产物,需保证process.env.NODE_ENV正确设置为production,避免Next.js以开发模式启动。
  • 调整端口监听逻辑:iisnode会自动处理端口分配,可将app.js中的端口设置改为port = process.env.PORT || 3000,或者监听0.0.0.0而非localhost,确保能接收来自IIS的请求:
    .listen(port, '0.0.0.0', () => {
        console.log(`> Ready on http://${hostname}:${port}`)
    })
    

5. 配置文件夹权限

  • 右键项目根文件夹 → 「属性」→ 「安全」,添加IIS应用池身份(格式为IIS AppPool\[你的站点名称]),赋予其「读取和执行」「列出文件夹内容」「读取」权限。
  • 确保.next、node_modules文件夹的权限继承自根目录,避免应用池用户无法访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 16:47:03