在AWS Windows服务器IIS+iisnode部署Next.js TS时页面显示server.js代码问题
问题分析与解决方案
你的核心问题是IIS未将请求转发给iisnode处理,而是直接返回了server.js的源码,导致Next.js页面无法渲染。以下是针对性修复方案:
1. 基础环境检查
- 确认IIS已安装并启用iisnode模块:打开IIS管理器→服务器节点→模块,查看是否有iisnode,缺失则重新下载安装
- 将站点应用程序池设置为无托管代码:右键应用程序池→高级设置→.NET CLR版本选择“无托管代码”
2. 修正web.config配置
你的web.config缺少关键的handler配置,且静态文件重写规则存在逻辑漏洞,以下是修改后的完整配置:
<configuration> <appSettings> <add key="NEXT_PUBLIC_WAITINGLIST_URL" value="http://localhost:8017/api/" /> <add key="WAITINGLIST_URL" value="http://localhost:8017/api" /> <add key="DATABASE_URL" value="somevalue" /> <add key="AUTH_SECRET" value="somevalue" /> </appSettings> <system.webServer> <!-- 新增:配置iisnode处理server.js请求 --> <handlers> <add name="iisnode" path="server.js" verb="*" modules="iisnode" /> </handlers> <iisnode node_env="production" nodeProcessCountPerApplication="1" maxConcurrentRequestsPerProcess="1024" maxNamedPipeConnectionRetry="100" namedPipeConnectionRetryDelay="250" maxNamedPipeConnectionPoolSize="512" maxNamedPipePooledConnectionAge="30000" asyncCompletionThreadCount="0" initialRequestBufferSize="4096" maxRequestBufferSize="65536" uncFileChangesPollingInterval="5000" gracefulShutdownTimeout="60000" loggingEnabled="true" logDirectory="iisnode" debuggingEnabled="true" debugHeaderEnabled="false" debuggerPortRange="5058-6058" debuggerPathSegment="debug" maxLogFileSizeInKB="128" maxTotalLogFileSizeInKB="1024" maxLogFiles="20" devErrorsEnabled="true" flushResponse="false" enableXFF="false" promoteServerVars="" configOverrides="iisnode.yml" watchedFiles="web.config;*.js" nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" /> <rewrite> <rules> <!-- 调试规则保留 --> <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> <match url="^server.js\/debug[\/]?" /> </rule> <!-- 修改静态文件规则:仅当public目录存在对应文件时才重写 --> <rule name="StaticContent" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_URI}" pattern="^/(.*)$" /> <add input="{DOCUMENT_ROOT}\public\{C:1}" matchType="IsFile" /> </conditions> <action type="Rewrite" url="public\{C:1}" /> </rule> <!-- 动态请求转发给server.js --> <rule name="DynamicContent" stopProcessing="true"> <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> <security> <requestFiltering> <hiddenSegments> <add segment="node_modules"/> </hiddenSegments> </requestFiltering> </security> <httpErrors existingResponse="PassThrough" /> </system.webServer> </configuration>
关键修改点说明:
- 新增
<handlers>节点:告诉IIS所有指向server.js的请求都由iisnode模块处理,这是解决源码直接返回的核心 - 调整
StaticContent规则:增加文件存在性检查,避免把Next.js路由请求误转发到public目录 - 完善
DynamicContent规则:排除目录请求,确保只有非文件非目录的请求才交给server.js处理
3. 确认Next.js构建状态
如果node_env设置为production,必须先在本地执行构建:
npm run build
再将.next、public、server.js、package.json等文件一起部署到IIS站点目录
4. server.js小调整
将端口配置改为仅使用环境变量(iisnode会自动分配端口):
const port = process.env.PORT; // 移除||3000
完成以上步骤后重启IIS站点,即可正常渲染Next.js页面。
内容的提问来源于stack exchange,提问作者usmannoor90
相关产品推荐
相关产品推荐

