Files
hometown/check-nginx-and-api.sh
2026-03-08 22:49:24 +08:00

96 lines
3.5 KiB
Bash
Raw Permalink 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本机 HTTP 80==========="
if command -v curl >/dev/null 2>&1; then
code_80=$(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 http://127.0.0.1/api/config (Host: view.airtep.com) => HTTP $code_80"
[ "$code_80" = "200" ] && echo "[OK] 80 反代正常。" || echo "[失败] 80 返回 $code_80"
fi
echo ""
echo "========== 4. 测试 HTTPS https://view.airtep.com/api/config ==========="
if command -v curl >/dev/null 2>&1; then
code_443=$(curl -sk -o /dev/null -w "%{http_code}" "https://view.airtep.com/api/config" 2>/dev/null)
echo "curl -sk https://view.airtep.com/api/config => HTTP $code_443"
if [ "$code_443" = "200" ]; then
echo "[OK] HTTPS 反代正常。"
else
echo "[失败] HTTPS 返回 $code_443。若 80 正常而 443 为 404说明 443 的 server 块内缺少 location /api 或顺序有误。"
fi
fi
echo ""
echo "========== 5. Nginx 中 location 顺序(前 20 个)==========="
if [ -n "$NGINX_CONF" ] && [ -f "$NGINX_CONF" ]; then
grep -n "location \|server {" "$NGINX_CONF" | head -25
fi