#!/bin/bash
# 快速修復 Nginx timeout

echo "修復 Nginx timeout 設置..."

# 備份當前配置
sudo cp /etc/nginx/sites-available/momo /etc/nginx/sites-available/momo.before-timeout-fix

# 修改 Nginx 配置，增加 timeout
sudo sed -i '/proxy_read_timeout/d' /etc/nginx/sites-available/momo
sudo sed -i '/proxy_connect_timeout/d' /etc/nginx/sites-available/momo
sudo sed -i '/proxy_send_timeout/d' /etc/nginx/sites-available/momo
sudo sed -i '/location \/ {/a\        proxy_read_timeout 300s;\n        proxy_connect_timeout 300s;\n        proxy_send_timeout 300s;' /etc/nginx/sites-available/momo

# 測試並重載
echo "測試 Nginx 配置..."
if sudo nginx -t; then
    echo "重載 Nginx..."
    sudo systemctl reload nginx
    echo "✅ Nginx timeout 已修復（300 秒）"
    echo ""
    echo "測試網址："
    echo "https://mo.wooo.work/sales_analysis"
else
    echo "❌ Nginx 配置錯誤，恢復備份"
    sudo cp /etc/nginx/sites-available/momo.before-timeout-fix /etc/nginx/sites-available/momo
fi
