server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    server_name _;

    # Write access and error logs to stdout/stderr so they appear in Railway logs
    access_log /dev/stdout;
    error_log  /dev/stderr debug;

    # Serve static assets directly; fall back to Laravel's front controller
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Pass PHP requests to PHP-FPM
    location ~ \.php$ {
        # Guard against passing non-existent scripts to PHP-FPM
        try_files $uri =404;

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;

        # Increase timeouts for long-running requests (must be inside location block)
        fastcgi_read_timeout 300;
        send_timeout 300;
    }

    # Deny access to hidden files (e.g. .env, .git)
    location ~ /\.(?!well-known).* {
        deny all;
    }

}
