Files
hometown/check-nginx-and-api.sh
2026-03-08 21:52:29 +08:00

82 lines
2.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 检查 Nginx 反代配置与本地 API5599是否正常
# 在服务器上执行: ./check-nginx-and-api.sh [Nginx站点配置路径]
# 例: ./check-nginx-and-api.sh /www/server/panel/vhost/nginx/view.airtep.com.conf
PORT="${PORT:-5599}"
NGINX_CONF="${1:-}"
echo "========== 1. 检查 API 是否监听并响应 =========="
if command -v ss >/dev/null 2>&1; then
if ss -tlnp 2>/dev/null | grep -q ":$PORT "; then
echo "端口 ${PORT} 已在监听。"
else
echo "端口 ${PORT} 未在监听,请先执行: ./start-api.sh 或 ./run.sh"
fi
fi
echo ""
echo "---------- curl http://127.0.0.1:${PORT}/config ----------"
if command -v curl >/dev/null 2>&1; then
code=$(curl -s -o /tmp/check-api-config.txt -w "%{http_code}" "http://127.0.0.1:${PORT}/config" 2>/dev/null)
echo "HTTP 状态码: $code"
if [ "$code" = "200" ]; then
echo "响应内容: $(cat /tmp/check-api-config.txt 2>/dev/null | head -c 200)"
echo ""
echo "[OK] 本机 API 正常。"
else
echo "[失败] 本机 API 未返回 200请启动后端: ./start-api.sh"
fi
rm -f /tmp/check-api-config.txt
else
echo "未安装 curl跳过。"
fi
echo ""
echo "========== 2. 检查 Nginx 反代配置 =========="
if [ -z "$NGINX_CONF" ]; then
echo "未传入 Nginx 配置路径。用法: $0 /www/server/panel/vhost/nginx/view.airtep.com.conf"
echo "请确认站点配置中包含:"
echo " location /api {"
echo " proxy_pass http://127.0.0.1:5599/; # 末尾必须有 /,且建议用 127.0.0.1 不用 localhost"
echo " ..."
echo " }"
exit 0
fi
if [ ! -f "$NGINX_CONF" ]; then
echo "文件不存在: $NGINX_CONF"
exit 1
fi
echo "配置文件: $NGINX_CONF"
echo ""
if grep -q "location /api" "$NGINX_CONF"; then
echo "--- location /api 片段 ---"
sed -n '/location \/api/,/^[[:space:]]*}/p' "$NGINX_CONF" | head -15
if grep -A1 "location /api" "$NGINX_CONF" | grep -q "proxy_pass http://127.0.0.1:5599/"; then
echo ""
echo "[OK] proxy_pass 为 http://127.0.0.1:5599/(正确)。"
elif grep -A1 "location /api" "$NGINX_CONF" | grep -q "proxy_pass.*5599"; then
echo ""
echo "[注意] 请将 proxy_pass 改为: http://127.0.0.1:5599/ (末尾加 /,并用 127.0.0.1"
else
echo ""
echo "[注意] 未找到 proxy_pass 到 5599请检查配置。"
fi
else
echo "[失败] 未找到 location /api请添加反代到 5599。"
fi
echo ""
echo "========== 3. 测试经 Nginx 访问 /api/config本机==========="
if command -v curl >/dev/null 2>&1; then
code_nginx=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1/api/config" -H "Host: view.airtep.com" 2>/dev/null)
echo "curl -H Host:view.airtep.com http://127.0.0.1/api/config => HTTP $code_nginx"
if [ "$code_nginx" = "200" ]; then
echo "[OK] 经 Nginx 反代访问 /api/config 正常。"
else
echo "[注意] 经 Nginx 返回 $code_nginx,请检查 Nginx 配置并重载: nginx -t && nginx -s reload"
fi
fi