###############################################################################
#  nginx.conf · 生产级主配置
###############################################################################
user  nginx;
worker_processes  auto;
worker_rlimit_nofile 65535;

error_log  /var/log/nginx/error.log  warn;
pid        /var/run/nginx.pid;

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

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

    # ------- 日志：带请求时长 / upstream 时长，方便排障 -------
    log_format  main  '$remote_addr - $remote_user [$time_local] '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent" '
                      'rt=$request_time uct="$upstream_connect_time" '
                      'urt="$upstream_response_time" '
                      'cache="$upstream_cache_status"';
    access_log  /var/log/nginx/access.log  main;

    # ------- 性能 -------
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout    65;
    keepalive_requests   1000;
    server_tokens   off;
    client_max_body_size 50m;

    # ------- gzip 压缩 -------
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 5;
    gzip_vary on;
    gzip_proxied any;
    gzip_types
        text/plain text/css text/xml
        application/json application/javascript application/xml+rss
        image/svg+xml;

    # ------- 限流：每 IP 每秒 20 个 API 请求 -------
    limit_req_zone $binary_remote_addr zone=api_limit:10m rate=20r/s;
    limit_req_status 429;

    # ------- proxy_cache 区 -------
    proxy_cache_path /var/cache/nginx
                     levels=1:2
                     keys_zone=api_cache:50m
                     max_size=2g
                     inactive=60m
                     use_temp_path=off;

    # ------- 上游池 / 业务 server 拆到子文件 -------
    include /etc/nginx/conf.d/*.conf;
}
