Files
vue-driven-cloud-storage/fix-nginx-upload-limit.sh
WanWanYun d7ac491ced 工具: 添加Nginx上传限制快速修复脚本
功能:
- 自动查找Nginx配置文件
- 自动备份原配置
- 修改上传大小限制为10GB
- 修改超时时间为1小时
- 测试并重载Nginx配置
- 失败时自动回滚

使用方法:
curl -fsSL https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/fix-nginx-upload-limit.sh | sudo bash

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-11 01:03:35 +08:00

131 lines
4.5 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上传限制快速修复脚本
# 将上传限制从默认值改为10GB超时时间改为1小时
################################################################################
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}╔═══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ 玩玩云 - Nginx上传限制快速修复脚本 ║${NC}"
echo -e "${BLUE}╚═══════════════════════════════════════════════════════════════╝${NC}"
echo ""
# 检查root权限
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}✗ 此脚本需要root权限运行${NC}"
echo -e "${YELLOW}请使用: sudo bash fix-nginx-upload-limit.sh${NC}"
exit 1
fi
# 查找Nginx配置文件
NGINX_CONF=""
if [[ -f /etc/nginx/sites-available/wanwanyun.conf ]]; then
NGINX_CONF="/etc/nginx/sites-available/wanwanyun.conf"
elif [[ -f /etc/nginx/conf.d/wanwanyun.conf ]]; then
NGINX_CONF="/etc/nginx/conf.d/wanwanyun.conf"
else
echo -e "${RED}✗ 未找到Nginx配置文件${NC}"
exit 1
fi
echo -e "${GREEN}✓ 找到配置文件: $NGINX_CONF${NC}"
echo ""
# 备份原配置
BACKUP_FILE="${NGINX_CONF}.backup-$(date +%Y%m%d-%H%M%S)"
cp "$NGINX_CONF" "$BACKUP_FILE"
echo -e "${GREEN}✓ 已备份配置到: $BACKUP_FILE${NC}"
echo ""
# 检查是否已经配置过
if grep -q "client_max_body_size 10G" "$NGINX_CONF"; then
echo -e "${YELLOW}⚠ 配置已经是10GB无需修改${NC}"
exit 0
fi
echo -e "${BLUE}正在修改Nginx配置...${NC}"
# 创建临时文件
TEMP_FILE=$(mktemp)
# 处理配置文件
awk '
/^server \{/ { in_server=1 }
in_server && /server_name/ && !added_size {
print
print ""
print " # 文件上传大小限制10GB"
print " client_max_body_size 10G;"
added_size=1
next
}
in_server && /location \/api/ { in_api=1 }
in_api && /proxy_set_header X-Forwarded-Proto/ {
print
print ""
print " # 上传超时设置大文件上传需要更长时间设置为1小时"
print " proxy_read_timeout 3600s;"
print " proxy_send_timeout 3600s;"
print " proxy_connect_timeout 300s;"
in_api=0
next
}
# 删除旧的超时设置
/proxy_read_timeout/ && in_api { next }
/proxy_send_timeout/ && in_api { next }
/proxy_connect_timeout/ && in_api { next }
# 删除旧的 client_max_body_size
/client_max_body_size/ { next }
{ print }
' "$NGINX_CONF" > "$TEMP_FILE"
# 替换原文件
mv "$TEMP_FILE" "$NGINX_CONF"
echo -e "${GREEN}✓ 配置已更新${NC}"
echo ""
# 测试Nginx配置
echo -e "${BLUE}测试Nginx配置...${NC}"
if nginx -t 2>&1 | grep -q "successful"; then
echo -e "${GREEN}✓ Nginx配置测试通过${NC}"
echo ""
# 重载Nginx
echo -e "${BLUE}重载Nginx服务...${NC}"
systemctl reload nginx
if [[ $? -eq 0 ]]; then
echo -e "${GREEN}✓ Nginx已重载${NC}"
echo ""
echo -e "${GREEN}╔═══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ✓ 修复完成! ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${BLUE}新的配置:${NC}"
echo " • 上传大小限制: 10GB"
echo " • 读取超时: 3600秒 (1小时)"
echo " • 发送超时: 3600秒 (1小时)"
echo " • 连接超时: 300秒 (5分钟)"
echo ""
echo -e "${YELLOW}注意: 请确保您的磁盘空间足够存储大文件!${NC}"
else
echo -e "${RED}✗ Nginx重载失败${NC}"
echo -e "${YELLOW}正在恢复备份...${NC}"
cp "$BACKUP_FILE" "$NGINX_CONF"
systemctl reload nginx
exit 1
fi
else
echo -e "${RED}✗ Nginx配置测试失败${NC}"
echo -e "${YELLOW}正在恢复备份...${NC}"
cp "$BACKUP_FILE" "$NGINX_CONF"
exit 1
fi