我一直在 Azure 應用服務 linux 上部署我的 fastify 應用。我正在使用 azure devops 將我的代碼推送到應用服務。我正在使用 8080 埠來運行我的應用程式,它在我的本地運行良好。 本地主機截圖
但是相同的代碼在我的 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 a””a”€a”€ / (GET)
2021-12-25T07:36:21.501681270Z a””a”€a”€ 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 站點影像
我在這個問題上搜索了很多,但沒有運氣。并且沒有關于這個問題的直接檔案。你們能指導我解決這個問題嗎?
謝謝。
uj5u.com熱心網友回復:
在 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()
筆記
.listenlocalhost默認系結到本地主機、、 介面(127.0.0.1或::1,取決于作業系統配置)。如果您在容器(Docker、GCP等)中運行 Fastify ,您可能需要系結到0.0.0.0. 決定監聽所有介面時要小心;它帶有固有的安全風險。有關更多資訊,請參閱檔案。
并確保您必須使用最新的 fastify 包版本。檢查 package.json 檔案中的版本。以下是
{
"name": "fastify",
"version": "3.0.0",
"main": "server.js",
...
}
更改為最新版本后,問題已解決。

使用Kudu 日志流查看詳細的日志資訊https://<your_app_name>.scm.azurewebsites.net/api/LogStream
參考這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395582.html
標籤:节点.js linux azure-web-app-service 固定
下一篇:Oracle移植自然JOIN
