server {
    listen 80;
    server_name _;

    # 若需在 Nginx 層強制 HTTPS（例如直連容器時），可取消註解以下區塊：
    # if ($http_x_forwarded_proto = "http") {
    #     return 301 https://$host$request_uri;
    # }

    root /var/www/public;
    index index.html index.php;

    charset utf-8;

    # ── Laravel pretty URLs ──────────────────────────────────────────
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

        fastcgi_buffering on;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 16 16k;
        fastcgi_read_timeout 300;
    }

    # ── Static asset caching ─────────────────────────────────────────
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # ── Block hidden files (.env, .git, etc.) ────────────────────────
    location ~ /\.(?!well-known).* {
        deny all;
    }

    # ── Disable favicon / robots.txt logging ─────────────────────────
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # ── Upload limit (match PHP post_max_size) ───────────────────────
    client_max_body_size 64M;

    # ── Error page → Laravel ─────────────────────────────────────────
    error_page 404 /index.php;
}
