You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Azure Web Service站点403空白页及SSH连接错误?

问题详情
  • 本地基于Express + Node.js 18开发的Web应用运行正常,部署到Azure Web App后页面空白,控制台返回403错误
  • 尝试SSH连接服务器时同样报错:
Pinging app default url...
Ping responded with status code: 403
Checking status, body: {"port":2222,"state":"STOPPED","canReachPort":false,"msg":"Unable to connect to WebApp"}
  • 遇到容器启动失败错误:Container didn't respond to HTTP pings on port 8080, failing site start
  • 已尝试修改Express监听端口为3000/443/8080,调整Azure的WEBSITE_PORT和PORT环境变量,均无效果
  • 当前核心Express代码片段:
import express from "express";
import session from "express-session";

const app = express(); // Create express app

app.listen(process.env.PORT); // Listen on port 8080
解决方案步骤

1. 修正Express监听配置

Azure Web App要求应用必须监听0.0.0.0而非默认的localhost,否则容器无法访问服务。修改代码:

import express from "express";
import session from "express-session";

const app = express();
const port = process.env.PORT || 8080;

// 添加根路由测试,确保有响应输出
app.get('/', (req, res) => {
  res.send('Hello Azure!');
});

// 必须绑定0.0.0.0地址
app.listen(port, '0.0.0.0', () => {
  console.log(`Server running on port ${port}`);
});

2. 确认Azure环境变量配置

  • 登录Azure门户,进入目标Web App
  • 左侧导航选择「配置」→「应用程序设置」
  • 确保添加或确认以下变量:
    • PORT: 8080(与代码默认端口一致)
    • WEBSITE_NODE_DEFAULT_VERSION: 18.x(明确指定Node版本,避免自动选择兼容问题)
  • 保存设置后,重启Web App

3. 自定义Dockerfile覆盖自动配置

若使用容器部署,Azure自动生成的Dockerfile可能存在端口匹配问题,可在项目根目录添加自定义Dockerfile:

FROM node:18-alpine

WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .

EXPOSE 8080
CMD ["node", "index.js"]  # 替换为你的实际入口文件名

部署时Azure会优先使用自定义Dockerfile,确保EXPOSE端口与代码、环境变量一致。

4. 查看日志定位深层问题

  • 在Azure门户Web App中,左侧导航选择「监控」→「日志流」,查看实时启动日志,确认是否有依赖缺失、代码报错等信息
  • 也可通过「高级工具」→「Kudu」进入站点管理页面,查看LogFiles目录下的详细错误日志

5. 修复SSH连接问题

SSH连接失败通常是应用未正常启动导致,解决上述应用启动问题后,SSH连接会自动恢复。若仍有异常,检查Web App的「配置」→「SSH」设置,确保SSH功能已启用。

内容的提问来源于stack exchange,提问作者SkyBlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:03:19