由于我想将Next.js应用程序部署到IIS上,所以我尝试在IIS上进行主机托管
如果你想部署Next.js,推荐使用Vercel,但如果你非常想在IIS上进行部署的话。
使用IIS扩展模块“HttpPlatformHandler”,可以启动node.exe并将HTTP请求处理给Node进程。
我们参考了下面的网站:
https://nodejs.keicode.com/nodejs/using-platformhandler-to-host-nodejs-on-iis.php
前期准备
由于需要IIS扩展模块,因此请安装。
https://www.iis.net/downloads/microsoft/httpplatformhandler
接下来,我会按照通常的方法创建一个Next.js应用,并完成构建。
我使用了create-next-app工具来创建应用。
我想在IIS的Web应用程序中,使http://localhost/nextapp这个URL可以访问,所以我在next.config.js中加入了basePath的设定。
module.exports = {
basePath: '/nextapp',
}
IIS的配置
创建Web应用程序”nextapp”,并在应用程序根目录下创建web.config文件。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add
name="httpPlatformHandler"
path="*"
verb="*"
modules="httpPlatformHandler"
resourceType="Unspecified" />
</handlers>
<httpPlatform
processPath="C:\Program Files\nodejs\node.exe"
arguments="C:\nextapp\node_modules\next\dist\bin\next start C:\nextapp">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
只要这个!
确认执行
请在浏览器中访问下面的链接:
http://localhost/nextapp
只要成功显示Next.js的界面,就算是成功了!
查看任务管理器,可以看到node.exe正常启动。
而且即使从多个会话连接,exe也不会增多,所以这方面应该是由IIS模块处理的。
最后/最终一点
我经过很多搜索都找不到,非常苦恼,如果能有人帮助我,我会非常感激。
不管怎样,在服务器上运行Next.js需要将源代码和node_modules等所有文件一起放在服务器上,是吗?
本来想用React实现,但为了实现SSR,就选择了Next.js,所以以为只需要像React一样将构建好的模块放在IIS应用程序根目录下即可运行。
如果有更好的方法,请务必告诉我。