# 4核服务器：设置 4 个 worker（或 auto 自动检测）
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    # 每个 worker 的最大连接数
    worker_connections 1024;
    # 同时接受多个连接
    multi_accept on;
    # 使用 epoll（Linux 高性能事件模型）
    use epoll;
}

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"';

    access_log /var/log/nginx/access.log main;

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

    # Gzip 压缩
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;

    server {
        listen 8000;
        server_name _;
        root /app/public;
        index index.php index.html;

        # 请求体大小限制（上传文件）
        client_max_body_size 100M;

        # Laravel 路由
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        # PHP-FPM 处理
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;

            # 超时设置
            fastcgi_connect_timeout 60s;
            fastcgi_send_timeout 300s;
            fastcgi_read_timeout 300s;

            # 缓冲区设置
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
        }

        # 静态文件缓存
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
            access_log off;
        }

        # 禁止访问隐藏文件
        location ~ /\. {
            deny all;
        }

        # 健康检查
        location /health {
            access_log off;
            return 200 "OK";
            add_header Content-Type text/plain;
        }
    }
}
