nginx 自定义错误页面并尝试 catch 不起作用

我目前正在尝试在页面因任何原因无法加载时显示自定义错误页面。

我首先尝试了 try catch 块,但不幸的是我无法用它捕获语法错误/致命错误,因为错误发生在代码过程中。因此,永远不会调用 catch 块。 但据我从 Stackoverflow 中的各种主题了解到,不幸的是,这在 PHP 中在技术上是不可行的。

所以现在我试图通过 NGINX 错误自定义页面来实现,但这似乎根本不起作用。我做了很多尝试和方法,但似乎都没有奏效。也看了几篇文章可惜没有成功。

有谁知道我在这里做错了什么? 这是我的配置文件。

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  domain.com;
    root         /usr/share/nginx/html/domain/public;
    index index.php index.html index.htm;

    ssl_certificate "/etc/nginx/ssl/bundle.crt";
    ssl_certificate_key "/etc/nginx/ssl/private.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    error_page 404 /404.html;
    location = /404.html {
        root /usr/share/nginx/html;
        internal;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            ssi on;
            root /usr/share/nginx/html/;
            index 50x.html
            internal;
    }
    proxy_intercept_errors on;

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

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

我使用以下规格: NGINX/1.20.1 PHP 8.0.8 CentOS 7.9

此外,我还运行了一个 Laravel 网站,它可以正确地识别这些类型的语法/致命错误,而无需进行任何进一步的配置?他们为此使用某种解析器吗?哪个在实现之前先检查代码?

提前致谢。

q775008 回答:nginx 自定义错误页面并尝试 catch 不起作用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2364.html

大家都在问