# AI Service Nginx Configuration
# 将此文件放置于 /etc/nginx/conf.d/ai-service.conf
# 或 include 到主配置文件中

# 后端 API 上游（供 Java 渠道侧调用）
upstream ai_service_backend {
    server 127.0.0.1:8182;
}

# 前端管理界面上游
upstream ai_service_admin {
    server 127.0.0.1:8181;
}

# 前端管理界面
server {
    listen 80;
    server_name your-domain.com;  # 替换为你的域名或服务器IP

    # 访问日志
    access_log /var/log/nginx/ai-service-admin.access.log;
    error_log /var/log/nginx/ai-service-admin.error.log;

    location / {
        proxy_pass http://ai_service_admin;
        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;
        proxy_cache_bypass $http_upgrade;

        # SSE 流式响应支持
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_buffering off;
    }
}

# 后端 API（供 Java 渠道侧调用）
# 如果使用域名，可以用不同的路径或子域名
# 示例：api.your-domain.com 或 your-domain.com/api/
server {
    listen 80;
    server_name api.your-domain.com;  # 替换为 API 子域名

    # 访问日志
    access_log /var/log/nginx/ai-service-api.access.log;
    error_log /var/log/nginx/ai-service-api.error.log;

    location / {
        proxy_pass http://ai_service_backend;
        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;
        proxy_cache_bypass $http_upgrade;

        # SSE 流式响应支持
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_buffering off;
    }
}

# ============================================================
# HTTPS 配置示例 (使用 Let's Encrypt)
# ============================================================

# server {
#     listen 443 ssl http2;
#     server_name your-domain.com;
#
#     ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
#
#     ssl_protocols TLSv1.2 TLSv1.3;
#     ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
#     ssl_prefer_server_ciphers off;
#
#     location / {
#         proxy_pass http://ai_service_admin;
#         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;
#         proxy_cache_bypass $http_upgrade;
#         proxy_read_timeout 300s;
#         proxy_connect_timeout 75s;
#         proxy_buffering off;
#     }
# }

# server {
#     listen 443 ssl http2;
#     server_name api.your-domain.com;
#
#     ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
#
#     ssl_protocols TLSv1.2 TLSv1.3;
#     ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
#     ssl_prefer_server_ciphers off;
#
#     location / {
#         proxy_pass http://ai_service_backend;
#         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;
#         proxy_cache_bypass $http_upgrade;
#         proxy_read_timeout 300s;
#         proxy_connect_timeout 75s;
#         proxy_buffering off;
#     }
# }

# HTTP 重定向到 HTTPS
# server {
#     listen 80;
#     server_name your-domain.com api.your-domain.com;
#     return 301 https://$server_name$request_uri;
# }
