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

如何在IIS服务器部署Next.js 13.4 App Router生产构建?报错求助

Next.js 13.4 App Router 生产构建部署到 IIS 解决方案

先排查本地生产构建可用性

先确认生产构建本身无问题:

  • 执行 npm run build 生成生产构建文件
  • 执行 npm start 启动生产服务器,本地访问 http://localhost:3000,确保应用能正常运行。如果本地无法访问,先修复构建环节的问题。

1. 修正 server.js 生产模式配置

明确强制生产模式启动,避免环境变量读取异常:

const next = require("next");
const http = require("http");

// 强制设置生产模式,指定当前目录确保找到.next构建文件夹
const app = next({ dev: false, dir: __dirname });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  // 直接使用IIS传递的PORT环境变量,无需硬编码
  const port = process.env.PORT;

  http
    .createServer((req, res) => {
      handle(req, res).catch((err) => {
        console.error("Error handling request:", err);
        res.status(500).send("Internal Server Error");
      });
    })
    .listen(port, () => {
      console.log(`Server listening on port ${port}`);
    });
});

2. 更新 web.config 适配生产构建

Next.js 生产静态资源存放在 .next/static,原配置的静态资源规则错误,调整后配置如下:

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" 
           scriptProcessor="C:\Program Files\iisnode\iisnode.dll" 
           resourceType="Unspecified" requireAccess="Script" 
           preCondition="bitness64" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^server.js\/debug[\/]?" />
        </rule>
        <!-- 优先处理Next.js静态资源 -->
        <rule name="NextStaticFiles" stopProcessing="true">
          <match url="^_next/static/(.*)" />
          <action type="Rewrite" url="{REQUEST_URI}" />
        </rule>
        <!-- 处理自定义静态文件 -->
        <rule name="StaticFiles" stopProcessing="true">
          <match url="^static/(.*)" />
          <action type="Rewrite" url="{REQUEST_URI}" />
        </rule>
        <!-- 所有动态请求转发到server.js -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="True" />
          </conditions>
          <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>
    <iisnode 
      watchedFiles="web.config;*.js;.next/**/*.js" 
      devErrorsEnabled="false" />
    <defaultDocument>
      <files>
        <add value="server.js" />
      </files>
    </defaultDocument>
    <directoryBrowse enabled="false" />
    <!-- 启用gzip压缩优化性能 -->
    <httpCompression>
      <dynamicTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
      </dynamicTypes>
      <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/json" enabled="true" />
        <add mimeType="image/*" enabled="true" />
      </staticTypes>
    </httpCompression>
  </system.webServer>
</configuration>

3. 配置 IIS 权限

  • 右键部署文件夹 → 属性 → 安全 → 编辑 → 添加用户 IIS AppPool\[你的应用池名称]
  • 给该用户分配 读取和执行、列出文件夹内容、读取 权限

4. 关于 Buffer 警告的说明

报错中的 DEP0005 警告是旧依赖库使用了废弃的 Buffer() 方法,不会直接导致 500 错误。可通过以下方式处理:

  • 执行 npm update 更新项目依赖(尤其是 Next.js 及相关包)
  • 若警告来自第三方库,暂时可忽略,等待库作者更新版本

部署步骤总结

  1. 将生产构建文件(server.js、package.json、.next 文件夹、node_modules)上传到 IIS 服务器部署目录
  2. 在 IIS 中创建网站,物理路径指向部署目录
  3. 应用池设置为 No Managed Code,.NET CLR 版本选择 无
  4. 重启 IIS 网站,访问测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 12:25:10