Search code examples
nginxhttp-status-codescustom-error-pages

How can I change the status code including custom error page


I'm changing the status code 403 to 404. I just don't want to let the user know that there is a restricted page at all. This works fine with the following config:

error_page 403 =404 @404;   

location @404 {
    return 404;
}

Now, I would like to return a custom 404 error page. But the status code sent to the user should always be 404 (when 403 occurs). How can I get both to work?

Edit:

I hoped sth. like this would work, but it doesn't.

location @404 {
    root /usr/share/nginx/html;
    internal;
    return 404 "/custom_404.html";
}

Solution

  • The problem was a deny statement. That statement is blocking everything including the access to any custom error page. The solution is to add allow all inside the custom error page block to revoke the inherited deny.

    error_page 403 404 =404 /custom_404.html;
    
    location = /custom_404.html {
        allow all;
        root /usr/share/nginx/html;
        internal;
    }
    

    original source:

    https://www.cyberciti.biz/faq/unix-linux-nginx-custom-error-403-page-configuration/