如何在 Azure 上部署 NextJs SSR React 应用程序

Jam*_*s D 3 reactjs server-side-rendering azure-web-app-service azure-pipelines next.js

我一直在尝试在 Azure 上部署使用 NextJS 构建的服务器端渲染的 React 应用程序。我设置了 Azure 管道并成功发布,但运行后,当我访问 azure 网站 URL 时,应用程序似乎没有加载。构建文件内容与客户端渲染的应用程序不同。请分享有关部署 SSR React 应用程序(在 Azure 上)的资源或说明。

我使用此资源来设置管道,没有遇到错误,但 URL 仍然未加载应用程序。

Gan*_*ula 9

正如 @James 在Doris 的回答中的评论中提到的,在 Next.js 应用程序中使用自定义server.js会使其生产速度变慢,因为路由不是预先构建的。使用next build后跟next start可以解决这个问题。但为了能够做到这一点,您不应该拥有server.js.

根据在 Azure Linux Web 应用程序上部署 Node JS / Next JS 应用程序的Microsoft 文档PM2,推荐的方法是使用而不是使用npm start(或)node server.js

所以你不需要server.jsweb.config。您需要做的就是使用ecosystem.config.js以下内容调用一个文件

module.exports = {
  apps: [
    {
      name: "my-nextJs-site",
      script: "./node_modules/next/dist/bin/next",
      args: "start -p " + (process.env.PORT || 3000),
      watch: false,
      autorestart: true,
    },
  ],
};
Run Code Online (Sandbox Code Playgroud)

并让你的Azure应用程序的启动命令是这样的

pm2 --no-daemon start /home/site/wwwroot/ecosystem.config.js
Run Code Online (Sandbox Code Playgroud)

package.json并且你的脚本没有改变

pm2 --no-daemon start /home/site/wwwroot/ecosystem.config.js
Run Code Online (Sandbox Code Playgroud)

Azure Windows 应用服务

pm2在 Azure Windows 服务中不可用 -它使用 IIS Server。查看以下答案及其链接的问题。

其他有用的资源: