mottery/fix_docker_nginx.sh

92 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.

#!/bin/bash
echo "🔧 修复Docker nginx配置..."
# 1. 检查当前配置
echo "📋 当前nginx配置:"
cat /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf
echo ""
echo "🔍 检查后端服务状态..."
# 2. 测试不同的后端地址
echo "🧪 测试后端连接..."
# 测试 host.docker.internal:8000
if curl -f http://host.docker.internal:8000/health > /dev/null 2>&1; then
echo "✅ host.docker.internal:8000 连接正常"
BACKEND_URL="http://host.docker.internal:8000/"
elif curl -f http://172.17.0.1:8000/health > /dev/null 2>&1; then
echo "✅ 172.17.0.1:8000 连接正常"
BACKEND_URL="http://172.17.0.1:8000/"
elif curl -f http://172.18.0.1:8000/health > /dev/null 2>&1; then
echo "✅ 172.18.0.1:8000 连接正常"
BACKEND_URL="http://172.18.0.1:8000/"
elif curl -f http://docker.for.mac.localhost:8000/health > /dev/null 2>&1; then
echo "✅ docker.for.mac.localhost:8000 连接正常"
BACKEND_URL="http://docker.for.mac.localhost:8000/"
else
echo "❌ 无法连接到后端服务"
echo "请检查后端服务是否正在运行"
exit 1
fi
# 3. 备份原配置
echo "💾 备份原配置..."
cp /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf.backup
# 4. 应用新配置
echo "⚙️ 应用新配置..."
cp cp.mzh.one.conf /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf
# 5. 更新后端地址
echo "🔧 更新后端地址为: $BACKEND_URL"
sed -i "s|proxy_pass http://host.docker.internal:8000/;|proxy_pass $BACKEND_URL;|g" /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf
sed -i "s|proxy_pass http://host.docker.internal:8000/health;|proxy_pass ${BACKEND_URL}health;|g" /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf
# 6. 测试nginx配置
echo "🔍 测试nginx配置..."
docker exec dnmp-nginx nginx -t
if [ $? -eq 0 ]; then
echo "✅ nginx配置正确"
# 7. 重新加载nginx
echo "🔄 重新加载nginx..."
docker exec dnmp-nginx nginx -s reload
echo "✅ nginx重新加载完成"
else
echo "❌ nginx配置错误恢复备份..."
cp /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf.backup /opt/dnmp/services/nginx/conf.d/cp.mzh.one.conf
exit 1
fi
# 8. 检查服务状态
echo "⏳ 等待服务启动..."
sleep 3
echo "🔍 检查服务状态..."
if curl -f https://cp.mzh.one/ > /dev/null 2>&1; then
echo "✅ 前端页面访问正常"
else
echo "❌ 前端页面访问异常"
fi
if curl -f https://cp.mzh.one/api/v1/lottery/ssq/latest > /dev/null 2>&1; then
echo "✅ API接口访问正常"
else
echo "❌ API接口访问异常"
fi
echo ""
echo "🎉 修复完成!"
echo "📋 访问地址: https://cp.mzh.one"
echo ""
echo "📋 如果仍有问题,请检查:"
echo " 1. 后端服务是否在端口8000运行"
echo " 2. Docker网络配置是否正确"
echo " 3. 防火墙设置"
echo ""
echo "📋 查看nginx错误日志:"
echo " docker exec dnmp-nginx tail -f /var/log/nginx/error.log"