【问题标题】:NGINX Custom error 500 page is not showingNGINX 自定义错误 500 页面未显示
【发布时间】:2020-02-26 22:22:34
【问题描述】:

我试图捕捉 NGINX 在/ 位置不存在或为空时显示的错误 500。

server {
    listen       80;
    server_name  localhost;
    root   /usr/share/nginx/html/dist/app;
    index  index.html index.htm;

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

    location / {
      try_files $uri $uri/ /index.html;
    }
}

当我保存并测试它时,NGINX 告诉我配置是有效的。 但是当我清除我的 dist 文件夹时,我得到了默认的 NGINX 错误 500。

ls -l/usr/share/nginx/html/

-rwxrwxrwx   1 root root     43 Feb 17 15:26 50x.html
drwxrwxrwx   2 root root   4096 Feb 17 15:32 dist

为什么我仍然从 NGINX 收到错误 500,而不是 50x.html 文档?

【问题讨论】:

    标签: nginx nginx-config


    【解决方案1】:

    假设 /index.html 是解释运行的东西(例如 PHP-FPM),NGINX 将默认显示来自 FastCGI 服务器的错误。

    要通过 NGINX 传递错误,您需要使用 fastcgi_intercept_errors

    fastcgi_intercept_errors off;
    

    类似地,如果您将请求代理到其他地方(FastCGI 服务器除外)的/index.html,您将使用proxy_intercept_errors

    proxy_intercept_errors off;
    

    【讨论】:

    • 不,这是一个角度应用程序。没有 CGI 后端,也没有反向代理。
    【解决方案2】:

    将你的 nginx 配置更改为

    server {
        listen       80 default_server;
        server_name  localhost;
        root   /usr/share/nginx/html/dist/app;
        index  index.html index.htm;
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
          ssi on;
          root /usr/share/nginx/html/;
          index 50x.html
          internal;
        }
    
        location / {
          try_files $uri $uri/ /index.html =500;
        }
    }
    

    您将能够看到自定义错误

    ➜  ~   curl http://127.0.0.1:32769/
    <!DOCTYPE html>
    <html>
    <head>
    <title>Error</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>This is My custome error.</h1>
    </body>
    </html>
    

    基本上你需要将索引添加到错误位置

     index 50x.html
    

    如果文件都不存在则抛出错误

    try_files $uri $uri/ /index.html =500;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多