# ======================================================================
# 生产环境网关 Nginx 核心配置文件与高并发访问日志样本
# 文件格式：.txt 纯文本 / 编码：UTF-8
# ======================================================================

--- [ SECTION 1: NGINX PRODUCTION CONFIGURATION ] ---
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 10240;
    use epoll;
    multi_accept on;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

    access_log /var/log/nginx/access.log main buffer=32k flush=5s;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # Gzip 压缩配置
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

    # 上游后端应用集群 (FastAPI 服务节点)
    upstream backend_cluster {
        server 172.28.0.10:8766 weight=5 max_fails=3 fail_timeout=10s;
        server 172.28.0.11:8766 weight=5 max_fails=3 fail_timeout=10s;
        keepalive 32;
    }

    server {
        listen 80;
        server_name api.studio.local;

        # 客户端最大包体限制 (允许知识库文件上传)
        client_max_body_size 100M;

        location / {
            proxy_pass http://backend_cluster;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            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;

            # 关键超时时间设置 (防 504 Gateway Timeout)
            proxy_connect_timeout 5s;
            proxy_read_timeout 60s;
            proxy_send_timeout 30s;
        }

        # SSE 流式输出专用长连接配置
        location /api/chat {
            proxy_pass http://backend_cluster;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_buffering off;
            proxy_cache off;
            proxy_read_timeout 300s;
            chunked_transfer_encoding on;
        }
    }
}

--- [ SECTION 2: PRODUCTION ACCESS LOG SAMPLES ] ---
192.168.1.101 - - [04/Nov/2026:09:15:20 +0800] "GET /api/config HTTP/1.1" 200 1420 "http://localhost:8766/" "Mozilla/5.0 Chrome/120.0.0.0" "-" rt=0.003 uct=0.001 uht=0.002 urt=0.003
192.168.1.102 - - [04/Nov/2026:09:15:22 +0800] "POST /api/chat HTTP/1.1" 200 4890 "http://localhost:8766/" "Mozilla/5.0 Chrome/120.0.0.0" "-" rt=2.341 uct=0.001 uht=0.150 urt=2.340
192.168.1.105 - - [04/Nov/2026:09:16:01 +0800] "GET /api/doc/02-系统架构图与调用拓扑.excalidraw HTTP/1.1" 200 3421 "http://localhost:8766/" "Mozilla/5.0 Chrome/120.0.0.0" "-" rt=0.005 uct=0.001 uht=0.004 urt=0.005
192.168.1.108 - - [04/Nov/2026:09:18:45 +0800] "POST /api/v1/orders/create HTTP/1.1" 200 562 "https://app.client.com" "MobileClient/3.2.0" "114.88.22.10" rt=0.082 uct=0.002 uht=0.080 urt=0.082
192.168.1.200 - - [04/Nov/2026:09:19:12 +0800] "GET /api/unknown-endpoint HTTP/1.1" 404 153 "-" "curl/7.88.1" "-" rt=0.001 uct=- uht=- urt=-
192.168.1.215 - - [04/Nov/2026:09:20:00 +0800] "POST /api/chat HTTP/1.1" 502 584 "http://localhost:8766/" "Mozilla/5.0 Chrome/120.0.0.0" "-" rt=5.002 uct=- uht=- urt=-
