server {
    listen 8080;
    server_name _;

    root /var/www/nis2-agile/public;
    index index.php index.html;

    charset utf-8;

    # ── 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;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # ── Logging ────────────────────────────────────────────────────────────
    access_log /var/log/nginx/nis2-access.log;
    error_log  /var/log/nginx/nis2-error.log;

    # ── Max Upload Size ────────────────────────────────────────────────────
    client_max_body_size 20M;

    # ── Main Location ──────────────────────────────────────────────────────
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # ── PHP-FPM Processing ─────────────────────────────────────────────────
    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        # Pass Authorization header (required for JWT auth)
        fastcgi_param HTTP_AUTHORIZATION $http_authorization;

        fastcgi_param HTTP_PROXY "";
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_read_timeout 300;
    }

    # ── Static Assets Caching ──────────────────────────────────────────────
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # ── Deny Hidden Files ──────────────────────────────────────────────────
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # ── Deny access to sensitive files ─────────────────────────────────────
    location ~* \.(env|sql|md|json|lock|yml|yaml)$ {
        deny all;
        access_log off;
        log_not_found off;
    }
}
