【问题标题】:Unable to deploy NodeJS (Fastify app) in Azure App Service Linux无法在 Azure 应用服务 Linux 中部署 NodeJS(Fastify 应用)
【发布时间】:2022-01-25 10:26:30
【问题描述】:

我一直在 Azure 应用服务 linux 上部署我的 fastify 应用。我正在使用 azure devops 将我的代码推送到应用服务。我正在使用 8080 端口运行我的应用程序,它在我的本地运行良好。 localhost screenshot

但相同的代码在我的 Azure 应用服务 linux 上不起作用。下面是我的 Azure 应用程序设置变量。

  {
    "name": "WEBSITE_NODE_DEFAULT_VERSION",
    "value": "14.15.1",
  },
  {
    "name": "WEBSITES_CONTAINER_START_TIME_LIMIT",
    "value": "400"
  },
  {
    "name": "WEBSITES_PORT",
    "value": "8080"
  }

我的 Fastify 启动代码

const start_server = async() => {
    try {
        const PORT = process.env.port || 8080;
        await fastify.listen(PORT, () => console.log('SERVER LISTENING AT PORT : '+ PORT));
    } catch (error) {
        fastify.log.error(error);
        process.exit(1);
    }
}
start_server();

Azure 日志流消息

2021-12-25T07:36:19.072325334Z > fastify_appserv@1.0.0 start /home/site/wwwroot
2021-12-25T07:36:19.072333734Z > node index
2021-12-25T07:36:19.072340734Z 
2021-12-25T07:36:21.501580169Z └── / (GET)
2021-12-25T07:36:21.501681270Z     └── health (GET)
2021-12-25T07:36:21.501695671Z 
2021-12-25T07:36:21.510859135Z {"level":30,"time":1640417781510,"pid":48,"hostname":"70cc318278d7","msg":"Server listening at http://127.0.0.1:8080"}
2021-12-25T07:36:21.511455845Z SERVER LISTENING AT PORT : 8080


2021-12-25T07:42:53.185Z ERROR - Container appservice-fastify_0_1efa2fe9 for site appservice-fastify did not start within expected time limit. Elapsed time = 400.3492937 sec
2021-12-25T07:42:53.188Z ERROR - Container appservice-fastify_0_1efa2fe9 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2021-12-25T07:42:53.213Z INFO  - Stopping site appservice-fastify because it failed during startup.
2021-12-25T07:44:08  No new trace in the past 1 min(s).
2021-12-25T07:45:08  No new trace in the past 2 min(s).
2021-12-25T07:46:08  No new trace in the past 3 min(s).
2021-12-25T07:47:08  No new trace in the past 4 min(s).
2021-12-25T07:48:08  No new trace in the past 5 min(s).
2021-12-25T07:49:08  No new trace in the past 6 min(s).
2021-12-25T07:50:08  No new trace in the past 7 min(s).
2021-12-25T07:51:08  No new trace in the past 8 min(s).
2021-12-25T07:52:08  No new trace in the past 9 min(s).
2021-12-25T07:53:08  No new trace in the past 10 min(s).

2021-12-25T07:53:35.981Z INFO  - 14-lts_20210810.1 Pulling from appsvc/node
2021-12-25T07:53:35.984Z INFO  -  Digest: sha256:235466ae6c6309188679f757798c5e15063c8206d4dec2fd919a5c279140def1
2021-12-25T07:53:35.986Z INFO  -  Status: Image is up to date for mcr.microsoft.com/appsvc/node:14-lts_20210810.1
2021-12-25T07:53:35.993Z INFO  - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
2021-12-25T07:53:36.052Z INFO  - Starting container for site
2021-12-25T07:53:36.053Z INFO  - docker run -d -p 8080:8080 --name appservice-fastify_0_f44cb2fd -e WEBSITES_PORT=8080 -e WEBSITE_SITE_NAME=appservice-fastify -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=appservice-fastify.azurewebsites.net -e WEBSITE_INSTANCE_ID=42864bd09e137c4d59228551c4bd55a9336afeee4f2ed34ea118913c305a339c appsvc/node:14-lts_20210810.1

当我在我的 azure 部署站点上导航到 /health 路线时,我收到以下消息 azure site image

我在这个问题上搜索了很多,但没有运气。并且没有关于这个问题的直接文档。 你们能指导我解决这个问题吗?

谢谢。

【问题讨论】:

    标签: node.js linux azure-web-app-service fastify


    【解决方案1】:

    在 Docker 内部,您应该明确提及 '0.0.0.0'。默认情况下fastify 只监听localhost 127.0.0.1 接口。要监听所有可用的 IPv4 接口,您应该修改为监听 0.0.0.0,所以我将其更改为以下内容

    const start = async () => {
        try {
            const PORT = process.env.port || 8080;
            await fastify.listen(PORT,'0.0.0.0', () => console.log('SERVER LISTENING AT PORT : '+ PORT))
        } catch (err) {
            fastify.log.error(err)
            process.exit(1)
        }
    }
    
    start()
    

    注意

    .listen绑定本地主机,localhost,默认接口 (127.0.0.1 或 ::1,取决于操作系统配置)。如果你在容器中运行 Fastify(Docker、GCP 等),你可能需要绑定到0.0.0.0。决定监听所有接口时要小心;它带有固有的security risks。请参阅the documentation 了解更多信息。

    并确保您必须使用最新的 fastify 包版本。检查 package.json 文件中的版本。接下来是

    {
        "name": "fastify",
        "version": "3.0.0",
        "main": "server.js",
        ...
    }
    

    更新到最新版本后,问题已修复。

    使用Kudu日志流查看详细日志信息https://<your_app_name>.scm.azurewebsites.net/api/LogStream

    参考here

    【讨论】:

    • 非常感谢。它现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 2019-03-09
    • 1970-01-01
    • 2022-06-13
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多