Custom 404 Page Not Displaying in Laravel with Nginx on Azure Hosting

I’m facing an issue where my custom 404 page is not displaying in a Laravel application hosted on Azure with Nginx. Instead, the default Nginx 404 page is shown. I’ve set up the configuration as follows:

/site/startup.sh

#!/bin/bash
cp /home/site/nginx.conf /etc/nginx/sites-available/default
service nginx reload

apt-get update -qq && apt-get install cron -yqq
(crontab -l 2>/dev/null; echo "* * * * * /usr/local/bin/php /home/site/wwwroot/artisan schedule:run >> /dev/null 2>&1")|crontab
service cron start

/site/nginx.conf

server {
    listen 8080;
    listen [::]:8080;
    listen 443 ssl;
    listen [::]:443 ssl;
    root /home/site/wwwroot/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index  index.php index.html index.htm;
    server_name  example.com; 
    port_in_redirect off;

    charset utf-8;

    location / {            
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ /.(?!well-known).* {
        deny all;
    }

    location ~ /.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~* [^/].php(/|$) {
        fastcgi_split_path_info ^(.+?.[Pp][Hh][Pp])(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_connect_timeout 300; 
        fastcgi_send_timeout 3600; 
        fastcgi_read_timeout 3600;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

    server_tokens off;
    client_max_body_size 256M;
}

Laravel Exception Handler:

public function render($request, Throwable $exception)
{
    if ($this->isHttpException($exception)) {
        if ($exception->getStatusCode() == 404) {
            return response()->view('frontend.test', [], 404); // If I change the status code to 200, the view can be rendered successfully.
        }
    }


    return parent::render($request, $exception);
}

What I’ve Tried:

  • Ensured error_page 404 /index.php; and fastcgi_intercept_errors on; is set in the Nginx configuration.
  • Cleared Laravel cache using php artisan config:clear, cache:clear, and view:clear.
  • Verified that the frontend.test view exists and is error-free.
  • Restarted Server.

Despite these efforts, the custom 404 page does not appear. Any insights or suggestions would be greatly appreciated!