#!/bin/bash # 配置远程服务器Nginx脚本 # 作者: emotion-museum # 日期: 2025-07-21 set -e REMOTE_HOST="root@47.111.10.27" NGINX_CONF_PATH="/www/server/nginx/conf" # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1" } log_error() { echo -e "${RED}[ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1" } # 检查Nginx状态 check_nginx() { log_info "检查Nginx状态..." ssh "$REMOTE_HOST" " echo '=== Nginx进程 ===' ps aux | grep nginx | grep -v grep || echo 'Nginx未运行' echo '' echo '=== Nginx配置目录 ===' ls -la $NGINX_CONF_PATH/ || echo 'Nginx配置目录不存在' echo '' echo '=== 端口80监听 ===' netstat -tlnp | grep :80 || echo '端口80未监听' " } # 备份现有配置 backup_nginx_config() { log_info "备份现有Nginx配置..." ssh "$REMOTE_HOST" " mkdir -p $NGINX_CONF_PATH/backup/\$(date +%Y%m%d_%H%M%S) cp $NGINX_CONF_PATH/nginx.conf $NGINX_CONF_PATH/backup/\$(date +%Y%m%d_%H%M%S)/ 2>/dev/null || true cp -r $NGINX_CONF_PATH/conf.d $NGINX_CONF_PATH/backup/\$(date +%Y%m%d_%H%M%S)/ 2>/dev/null || true " log_success "Nginx配置已备份" } # 创建情感博物馆站点配置 create_site_config() { log_info "创建情感博物馆站点配置..." ssh "$REMOTE_HOST" "cat > $NGINX_CONF_PATH/conf.d/emotion-museum.conf << 'EOF' # 情感博物馆站点配置 server { listen 80; server_name 47.111.10.27; # 前端静态文件 location /emotion-museum { alias /data/www/emotion-museum; index index.html; try_files \$uri \$uri/ /emotion-museum/index.html; # 静态资源缓存 location ~* \\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)\$ { expires 1y; add_header Cache-Control \"public, immutable\"; } } # API网关代理 location /api/ { proxy_pass http://localhost:19000/; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; # 超时设置 proxy_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; } # WebSocket代理 location /ws/ { proxy_pass http://localhost:19007/; proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection \"upgrade\"; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; } # 健康检查 location /health { access_log off; return 200 \"healthy\"; add_header Content-Type text/plain; } # 日志配置 access_log /var/log/nginx/emotion-museum.access.log; error_log /var/log/nginx/emotion-museum.error.log; } # 微服务直接访问(用于调试) server { listen 80; server_name api.47.111.10.27; # 网关服务 location /gateway/ { proxy_pass http://localhost:19000/; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; } # 用户服务 location /user/ { proxy_pass http://localhost:19001/; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; } # AI服务 location /ai/ { proxy_pass http://localhost:19002/; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; } # 认证服务 location /auth/ { proxy_pass http://localhost:19008/; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; } } EOF" log_success "站点配置创建完成" } # 测试Nginx配置 test_nginx_config() { log_info "测试Nginx配置..." if ssh "$REMOTE_HOST" "nginx -t"; then log_success "Nginx配置测试通过" else log_error "Nginx配置测试失败" return 1 fi } # 重载Nginx配置 reload_nginx() { log_info "重载Nginx配置..." if ssh "$REMOTE_HOST" "systemctl reload nginx"; then log_success "Nginx配置重载成功" else log_warning "Nginx重载失败,尝试重启..." if ssh "$REMOTE_HOST" "systemctl restart nginx"; then log_success "Nginx重启成功" else log_error "Nginx重启失败" return 1 fi fi } # 检查配置结果 check_result() { log_info "检查配置结果..." ssh "$REMOTE_HOST" " echo '=== Nginx状态 ===' systemctl status nginx --no-pager -l echo '' echo '=== 端口监听 ===' netstat -tlnp | grep :80 echo '' echo '=== 测试访问 ===' curl -I http://localhost/health 2>/dev/null || echo '健康检查失败' " } # 主函数 main() { log_info "🔧 开始配置远程服务器Nginx..." # 检查SSH连接 if ! ssh -o ConnectTimeout=10 "$REMOTE_HOST" "echo 'SSH连接成功'" > /dev/null 2>&1; then log_error "无法连接到远程服务器: $REMOTE_HOST" exit 1 fi check_nginx backup_nginx_config create_site_config test_nginx_config reload_nginx check_result log_success "🎉 Nginx配置完成!" echo "" echo "📋 访问地址:" echo " 前端应用: http://47.111.10.27/emotion-museum" echo " API接口: http://47.111.10.27/api/" echo " WebSocket: ws://47.111.10.27/ws/" echo " 健康检查: http://47.111.10.27/health" echo "" echo "🔧 调试地址:" echo " 网关服务: http://api.47.111.10.27/gateway/" echo " 用户服务: http://api.47.111.10.27/user/" echo " AI服务: http://api.47.111.10.27/ai/" echo " 认证服务: http://api.47.111.10.27/auth/" } # 执行主函数 main "$@"