首页 > 解决方案 > 无法在 Azure 应用服务上运行 Next.js 应用

问题描述

我正在尝试让最基本的 Next.js 应用在 Azure 应用服务中运行(URL 是https://asmkp-rich-test2.azurewebsites.net/)。

我可以让一个基本的 Node 应用程序运行良好,从 git 部署(存储库在这里:https ://github.com/Richiban/nextjs-azure-test ,分支是release)。

但是,我所做的任何事情都不会让这个应用程序运行。

如果您点击该 URL,您将看到:

The page cannot be displayed because an internal server error has occurred.

std 输出中没有任何内容,除了:

Node version: v10.6.0
Done
[5:45:37 PM] Compiling server
[5:45:38 PM] Compiling client
[5:45:38 PM] Compiled server in 1000ms
[5:45:41 PM] Compiled client in 3s
 DONE  Compiled successfully in 3859ms5:45:41 PM

App prepared
Got handle
Ready on http://localhost:8080

除了以下内容之外,错误输出中没有任何内容:

(node:22980) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

你可以在 git repo 中看到我server.js的,但我也会在此处包含它以方便阅读:

const http = require("http");
const next = require("next");
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });

console.log("Node version: " + process.version);

app.prepare()
    .then(() => {
        console.log("App prepared");

        const handle = app.getRequestHandler();

        console.log("Got handle");

        http.createServer(function(req, res) {
            console.log(`Processing incoming request`);

            try {
                handle(req, res).catch(function(e) {
                    console.log(`Error caught3: ` + e);
                    console.log(e);
                });
                
                console.log(`Incoming request done`);
            } catch (e) {
                console.log(`Error caught: ` + e);
                console.log(e);
            }
        }).listen(port);

        console.log(`> Ready on http://localhost:${port}`);
    })
    .catch(function(e) {
        console.log(`Error caught2: ` + e);
        console.log(e);
    });

console.log("Done");

正如您可能从我塞在那里的大量日志中看到的那样,我今天对此束手无策。

所以,总而言之,我有一个最简单的 Next.js 应用程序,我正在使用 Git 将它部署到 Azure 应用程序服务,虽然它在我的机器上运行得很好,但在 Azure 中我收到一条看起来像 no 的毫无意义的错误消息更多细节。

请帮忙。

标签: node.jsazureazure-web-app-servicenext.js

解决方案


DeprecationWarning 是一个红鲱鱼。您看到此一般错误是因为 iisnode 无法与节点进程通信。

process.env.PORT 在使用 iisnode 时实际上是一个管道名称,因此 parseInt 失败并使用您的后备端口 3000。这意味着您的 next.js 应用程序正在侦听错误的端口。相应地更新您设置端口号的行:

const port = process.env.PORT;


推荐阅读