Next.js部署IIS问题:无.html后缀子页面刷新/直访404
解决Next.js 14.1.3 App Router部署IIS刷新/直接访问子页面404问题
1. 修正web.config的URL重写规则
确保所有非静态资源请求都转发到server.js,同时排除静态文件让IIS直接处理。替换现有web.config为以下配置(注意调整node.exe路径):
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="iisnode" path="server.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <!-- 静态资源直接由IIS处理,停止后续规则 --> <rule name="SkipStaticFiles" stopProcessing="true"> <match url=".*\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp)$" /> <action type="None" /> </rule> <!-- 非文件/非目录请求转发到server.js --> <rule name="ForwardToNextServer"> <match url="^(.*)$" /> <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> <!-- 指定node.exe路径,根据你的安装位置调整 --> <iisnode nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe"" /> </system.webServer> </configuration>
2. 确认next.config.js的输出配置
使用App Router必须开启standalone输出模式,确保打包后的项目包含完整的server端逻辑:
/** @type {import('next').NextConfig} */ const nextConfig = { output: 'standalone', trailingSlash: false, // 保持URL无后缀/斜杠,匹配你的需求 // 如果应用部署在子路径,添加basePath: '/your-subpath' } module.exports = nextConfig
打包后,将.next/standalone目录下的所有文件部署到IIS网站的物理路径,不要直接部署项目根目录。
3. 排查静态HTML文件干扰
检查部署目录下是否存在about.html这类静态文件,直接删除它们。App Router的路由逻辑应该由server.js处理,静态HTML文件会优先被IIS识别,导致无后缀URL无法触发Next.js路由。
4. 验证IIS配置细节
- 确认应用池的
.NET CLR版本设置为无托管代码。 - 查看
iisnode\logs目录下的日志,检查server.js是否正常启动,请求转发时是否有报错。 - 使用IIS的URL Rewrite测试工具:选中网站 → 打开URL Rewrite → 右侧“测试模式”,输入
/about,确认规则会将请求重写到server.js。
内容的提问来源于stack exchange,提问作者Koen de vulder
相关产品推荐
相关产品推荐

