# Nginx配置示例，用于生产环境反向代理
# 将此文件放置在 /etc/nginx/sites-available/ 并创建符号链接到 sites-enabled/

upstream nextjs_app {
    server localhost:3000;
}

upstream websocket_app {
    server localhost:3001;
}

server {
    listen 80;
    server_name your-domain.com;
    
    # 如果使用HTTPS，取消下面的注释并配置SSL证书
    # listen 443 ssl;
    # ssl_certificate /path/to/ssl/cert.pem;
    # ssl_certificate_key /path/to/ssl/key.pem;
    
    # 增加请求体大小限制
    client_max_body_size 100M;
    
    # Next.js应用的主要路由
    location / {
        proxy_pass http://nextjs_app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        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;
    }
    
    # WebSocket专用路由
    location /ws-api {
        proxy_pass http://websocket_app;
        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;
        
        # WebSocket特定的超时设置
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }
    
    # 静态资源缓存
    location /_next/static {
        proxy_pass http://nextjs_app;
        proxy_cache_valid 60m;
        add_header Cache-Control "public, immutable";
    }
    
    location /public {
        proxy_pass http://nextjs_app;
        proxy_cache_valid 60m;
        add_header Cache-Control "public, max-age=3600";
    }
}






