server {
    listen 8080 default_server;
    server_name _;

    root /usr/share/nginx/html;
    index index.html;

    charset utf-8;

    # --- Real IP (Apache reverse proxy en amont) ---
    set_real_ip_from 127.0.0.1;
    set_real_ip_from 172.16.0.0/12;   # docker bridge
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

    # --- Compression ---
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types
        text/plain
        text/css
        text/xml
        application/javascript
        application/json
        application/xml
        application/rss+xml
        application/atom+xml
        image/svg+xml
        font/woff2;

    # --- Security headers ---
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=(), interest-cohort=()" always;

    # --- Long cache pour les assets fingerprintes (Astro _assets/) ---
    location ^~ /_assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable" always;
        access_log off;
        try_files $uri =404;
    }

    # --- Cache moyen pour autres statiques non fingerprintes ---
    location ~* \.(?:jpg|jpeg|png|webp|avif|gif|svg|ico|woff2|woff|ttf|otf)$ {
        expires 30d;
        add_header Cache-Control "public" always;
        access_log off;
        try_files $uri =404;
    }

    # --- robots.txt / sitemap : pas de log d'acces ---
    location = /robots.txt { access_log off; log_not_found off; }
    location = /sitemap.xml { access_log off; log_not_found off; }
    location = /sitemap-index.xml { access_log off; log_not_found off; }
    location = /favicon.svg { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }

    # --- URLs propres (Astro format: 'file' + trailingSlash: 'never') ---
    # Cherche /foo, puis /foo.html, puis /foo/index.html
    location / {
        try_files $uri $uri.html $uri/index.html =404;
    }

    # --- Page 404 personnalisee ---
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    # --- Bloque acces aux fichiers caches ---
    location ~ /\.(?!well-known) {
        deny all;
        access_log off;
        log_not_found off;
    }
}
