# =============================================================================
# Configuração Global (Escopo Main)
# =============================================================================
# user www www; # Comentado para rodar em modo não-root
worker_processes auto;
pid /tmp/nginx.pid;
error_log /var/log/nginx/error.log warn;

# Número máximo de conexões simultâneas
events {
    worker_connections 2048;
    use epoll;
    multi_accept on;
}

# =============================================================================
# Escopo HTTP (Onde os caminhos temporários DEVEM ficar)
# =============================================================================
http {
    # Caminhos temporários para execução rootless segura
    client_body_temp_path /tmp/client_temp;
    proxy_temp_path       /tmp/proxy_temp_path;
    fastcgi_temp_path     /tmp/fastcgi_temp;
    uwsgi_temp_path       /tmp/uwsgi_temp;
    scgi_temp_path        /tmp/scgi_temp;

    # MIME types
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging format
    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;

    # Performance
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;

    # FastCGI Cache Path
    fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=SELO_CACHE:10m max_size=100m inactive=60s use_temp_path=off;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";

    # Buffer sizes
    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 100M;
    large_client_header_buffers 4 32k;

    # Timeouts
    client_body_timeout 12;
    client_header_timeout 12;
    send_timeout 10;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 1000;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml
        application/xml+rss
        application/x-javascript
        image/svg+xml;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Esconder versão do Nginx
    server_tokens off;

    # Incluir configurações de sites
    include /etc/nginx/conf.d/*.conf;
}
