# 结伴客后端Nginx配置文件
# SSL配置和wapi.jiebanke.com域名设置

# HTTP服务器配置 - 重定向到HTTPS
server {
    listen 80;
    server_name wapi.jiebanke.com;
    
    # 强制HTTPS重定向
    return 301 https://$server_name$request_uri;
}

# HTTPS服务器配置
server {
    # 监听443端口并启用SSL
    listen 443 ssl http2;
    server_name wapi.jiebanke.com;
    
    # SSL证书配置
    ssl_certificate /etc/nginx/ssl/wapi.jiebanke.com.crt;
    ssl_certificate_key /etc/nginx/ssl/wapi.jiebanke.com.key;
    
    # SSL优化配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    
    # HSTS配置
    add_header Strict-Transport-Security "max-age=63072000" always;
    
    # 安全头部配置
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header Referrer-Policy "no-referrer-when-downgrade";
    
    # 访问日志配置
    access_log /var/log/nginx/webapi_access.log;
    error_log /var/log/nginx/webapi_error.log;
    
    # 代理配置
    location / {
        # 代理到Node.js后端服务
        proxy_pass http://localhost:3310;
        
        # 代理头信息配置
        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_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        
        # 代理超时设置
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 120s;
        
        # 缓冲区设置
        proxy_buffering on;
        proxy_buffer_size 8k;
        proxy_buffers 8 16k;
        proxy_busy_buffers_size 16k;
    }
    
    # 健康检查端点
    location /health {
        access_log off;
        proxy_pass http://localhost:3310/health;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    # API文档端点
    location /api-docs {
        proxy_pass http://localhost:3310/api-docs;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    # 静态资源缓存控制（如果后端有静态资源）
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        proxy_pass http://localhost:3310;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
    
    # 错误页面配置
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

# 可选：负载均衡配置（如果有多个后端实例）
# upstream jiebanke_backend {
#     server localhost:3200;
#     # 可以添加更多后端实例
#     # server localhost:3201;
#     # server localhost:3202;
#     
#     # 负载均衡策略
#     # least_conn;
#     # ip_hash;
# }