GRX
GRX

Reputation: 531

NGINX Custom error 500 page is not showing

I'm trying to catch the error 500 that NGINX shows when the / location doesn't exist or is empty.

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;
    }
}

When I save and test this, NGINX tells me the configuration is valid. But then when I clear my dist folder, I get the default NGINX error 500.

ls -l of /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

How is it that I still get the error 500 from NGINX, instead of the 50x.html doc?

Upvotes: 1

Views: 10132

Answers (2)

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2855

change your nginx configs to

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;
    }
}

and you will be able to see the custom error

➜  ~   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>

basically you need to add the index to the error location

 index 50x.html

and throw an error if none of the files exists

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

Upvotes: 6

Danila Vershinin
Danila Vershinin

Reputation: 9895

Assuming that /index.html is something that runs interpreted (e.g. PHP-FPM), NGINX will default to showing errors from FastCGI server.

To deliver errors by NGINX, you would want to use fastcgi_intercept_errors:

fastcgi_intercept_errors off;

Similiarly, if you're proxying request to /index.html elsewhere (other than a FastCGI server), you would use proxy_intercept_errors:

proxy_intercept_errors off;

Upvotes: 4

Related Questions