# ==========================================
# Florence-2 物体检测 API - Nginx 配置
# ==========================================
# 
# 使用方法：
# 1. SSH 到服务器：ssh -p 5204 root@156.225.27.52
# 2. 编辑 Nginx 配置：sudo nano /etc/nginx/sites-available/ai-chatbot
# 3. 在 server 块中添加以下内容（在其他 location 块之前）
# 4. 测试配置：sudo nginx -t
# 5. 重载 Nginx：sudo systemctl reload nginx

# ==========================================
# 添加到 server 块中（在其他 location 之前）
# ==========================================

    # Florence-2 物体检测 API 代理
    location /api/detect-object {
        # 代理到本地 8002 端口
        proxy_pass http://127.0.0.1:8002/api/detect-object;
        
        # HTTP 版本
        proxy_http_version 1.1;
        
        # 必要的请求头
        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;
        
        # 禁用缓存（AI 推理结果不应缓存）
        proxy_cache_bypass $http_upgrade;
        proxy_no_cache 1;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
        
        # 超时设置（AI 推理可能需要时间）
        proxy_connect_timeout 10s;
        proxy_send_timeout 10s;
        proxy_read_timeout 10s;
        
        # 错误处理
        proxy_intercept_errors on;
        error_page 502 503 504 = @florence_error;
    }
    
    # 错误处理位置块
    location @florence_error {
        default_type application/json;
        return 502 '{"success":false,"data":null,"error":"物体识别服务暂时不可用，请稍后重试"}';
    }

# ==========================================
# 完整示例（参考）
# ==========================================
# 
# server {
#     listen 443 ssl http2;
#     server_name ai.yydd.ai;
#     
#     # SSL 配置...
#     
#     # Florence-2 API（添加在最前面）
#     location /api/detect-object {
#         proxy_pass http://127.0.0.1:8002/api/detect-object;
#         proxy_http_version 1.1;
#         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_connect_timeout 10s;
#         proxy_read_timeout 10s;
#     }
#     
#     # 其他 location 块...
#     location /api/ {
#         proxy_pass http://127.0.0.1:3000;
#         ...
#     }
#     
#     location / {
#         proxy_pass http://127.0.0.1:3000;
#         ...
#     }
# }

# ==========================================
# 验证配置
# ==========================================
# 
# 1. 测试后端直接访问（应该成功）：
#    curl http://localhost:8002/health
#    预期：{"status":"ok","model_loaded":true}
# 
# 2. 测试 Nginx 代理（应该成功）：
#    curl https://ai.yydd.ai/api/detect-object -X POST \
#      -H "Content-Type: application/json" \
#      -d '{"imageUrl":"https://example.com/test.jpg","clickX":100,"clickY":100,"imageIndex":1,"imageWidth":500,"imageHeight":500}'
#    预期：返回 JSON（可能是错误，但不应该是 404）
# 
# 3. 查看 Nginx 日志：
#    sudo tail -f /var/log/nginx/access.log
#    sudo tail -f /var/log/nginx/error.log

