我正在尝试在 IIS 上发布 NextJS 网站,我已经在服务器上安装了 URL Rewrite 和 IIS Node,我尝试按照 本指南设置反向代理,但没有成功似乎有效,因为即使我在服务器上运行该项目,它也没有重定向到它。
然后我尝试了使用自定义服务器托管,这在本地运行时有效,但是当运行
next build
时,将生成的.next文件夹、package.json、web.config和server.js复制到IIS所在的服务器上正在托管,运行 node server.js
似乎不起作用,因为它只是在以下时间后停止:
info - SWC minify release candidate enabled. https://nextjs.link/swcmin
我看到的其他问题或博客似乎没有涵盖在与用于开发的计算机不同的计算机上托管,因此我不清楚是否应该将整个源代码复制到生产服务器,并且在那里运行
next start
,这看起来不太理想。
终于有时间重温一下这个了。因此,请扩展我上面的评论。
假设您使用Next.js的官方示例,您只需要执行以下步骤即可,
npm run build
创建生产工件。字面意思相当于跑步
npm run start
。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="<some path>\node.exe" arguments=".\node_modules\next\dist\bin\next start">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
您可以从源文件夹中删除不需要的文件,并保留
.next
、node_modules
和 web.config
。
https://halfblood.pro/running-nodejs-web-apps-on-iis-with-httpplatformhandler/#nextjs