Nginx Custom error page for Individual directory

1.7k Views Asked by At

This my nginx configuration for 404 custom error page. But my problem is I'm getting custom 404 error page for all my 404 error that occurs in my server. I want to get custom error page for a particular directory only.

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

    index index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        root /var/www/html;
        try_files $uri $uri/ =404;
        }

    error_page 404 /fail.html;

    location = /fail.html {
        root /var/www/html/checking/errors;
        internal;
        }
    location /testing/([a-zA-Z]+)/ {
        root /var/www/html/testing/$1;
        try_files $uri $uri.html;
        }
    }
1

There are 1 best solutions below

0
On

Assuming that what you're asking for is that when people attempt to view files of the form /testing/does-not-exist then they will see you error file, you want to move the error_page 404 /fail.html to within that location block. Where you have it right now (within the server block), it will apply to all requests. If it's for a different path that you'd like the error to show up, create a new location block for that path and then add the error_page directive to that block.