peixue-dev/peidu/Archive/一次性文件/[一次性]修复Nginx配置-2026-01-26.sh

116 lines
3.6 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
# ========================================
# Nginx配置修复脚本 - 解决uploads路径404问题
# ========================================
echo "========================================"
echo "🔧 开始修复Nginx配置"
echo "========================================"
# 1. 备份当前配置
echo "📦 备份当前配置..."
cp /www/server/panel/vhost/nginx/px.ddn-ai.cloud.conf \
/www/server/panel/vhost/nginx/px.ddn-ai.cloud.conf.backup.$(date +%Y%m%d_%H%M%S)
# 2. 检查是否有重复的location /uploads/
echo ""
echo "🔍 检查重复的location块..."
DUPLICATE_COUNT=$(grep -c "location /uploads/" /www/server/panel/vhost/nginx/px.ddn-ai.cloud.conf)
echo "找到 $DUPLICATE_COUNT 个 'location /uploads/' 块"
if [ $DUPLICATE_COUNT -gt 1 ]; then
echo "⚠️ 发现重复配置,需要修复"
fi
# 3. 检查Image目录
echo ""
echo "📁 检查Image目录..."
if [ -d "/www/wwwroot/px.ddn-ai.cloud/Image" ]; then
echo "✅ Image目录存在"
ls -lh /www/wwwroot/px.ddn-ai.cloud/Image/ | head -5
else
echo "❌ Image目录不存在"
exit 1
fi
# 4. 检查目录权限
echo ""
echo "🔐 检查目录权限..."
ls -ld /www/wwwroot/px.ddn-ai.cloud/Image/
PERM=$(stat -c %a /www/wwwroot/px.ddn-ai.cloud/Image/)
if [ "$PERM" != "755" ]; then
echo "⚠️ 权限不正确,正在修复..."
chmod 755 /www/wwwroot/px.ddn-ai.cloud/Image/
echo "✅ 权限已修复为755"
fi
# 5. 检查文件权限
echo ""
echo "🔐 检查文件权限..."
FILE_COUNT=$(find /www/wwwroot/px.ddn-ai.cloud/Image/ -type f | wc -l)
echo "找到 $FILE_COUNT 个文件"
if [ $FILE_COUNT -gt 0 ]; then
FIRST_FILE=$(find /www/wwwroot/px.ddn-ai.cloud/Image/ -type f | head -1)
ls -l "$FIRST_FILE"
FILE_PERM=$(stat -c %a "$FIRST_FILE")
if [ "$FILE_PERM" != "644" ]; then
echo "⚠️ 文件权限不正确,正在修复..."
find /www/wwwroot/px.ddn-ai.cloud/Image/ -type f -exec chmod 644 {} \;
echo "✅ 文件权限已修复为644"
fi
fi
# 6. 显示当前Nginx配置中的uploads部分
echo ""
echo "========================================"
echo "📄 当前Nginx配置中的uploads部分"
echo "========================================"
grep -A 5 "location /uploads/" /www/server/panel/vhost/nginx/px.ddn-ai.cloud.conf
# 7. 测试Nginx配置
echo ""
echo "========================================"
echo "🧪 测试Nginx配置..."
echo "========================================"
nginx -t
if [ $? -eq 0 ]; then
echo ""
echo "✅ Nginx配置测试通过"
echo ""
echo "是否重载Nginx配置(y/n)"
read -r RELOAD
if [ "$RELOAD" = "y" ]; then
nginx -s reload
echo "✅ Nginx已重载"
fi
else
echo ""
echo "❌ Nginx配置测试失败"
echo "请手动检查配置文件"
fi
# 8. 测试文件访问
echo ""
echo "========================================"
echo "🧪 测试文件访问..."
echo "========================================"
if [ $FILE_COUNT -gt 0 ]; then
TEST_FILE=$(find /www/wwwroot/px.ddn-ai.cloud/Image/ -type f -name "*.jpg" -o -name "*.jpeg" -o -name "*.png" | head -1)
if [ -n "$TEST_FILE" ]; then
FILENAME=$(basename "$TEST_FILE")
echo "测试文件: $FILENAME"
echo ""
echo "本地访问测试:"
curl -I "http://localhost/uploads/$FILENAME" 2>&1 | head -10
echo ""
echo "外部访问测试:"
curl -I "https://px.ddn-ai.cloud/uploads/$FILENAME" 2>&1 | head -10
fi
fi
echo ""
echo "========================================"
echo "✅ 诊断完成"
echo "========================================"