1

I have defined in my server block:

 server {

  error_page 401 403 404 /custom_404.html;
        location = /custom_404.html {
                root /var/www/html;
                internal;
        }

  error_page 500 502 503 504 /custom_50x.html;
        location = /custom_50x.html {
                root /var/www/html;
                internal;
        }

}

However, whenever there is a 404 error, this doesnt go through Nginx but it goes through Wordpress redirect.

Same happens with 500 errors. The error page I have defined in Nginx, is not displayed at all. I am getting the default 500 blank screen.

Any ideas on what could be wrong? I believe my code is correct.

  • If WordPress is correctly configured/installed, then WP will process al the input, because everything is redirected to index.php and therefore WordPress also handles the error pages. Look at my answer below to add your own WP error pages. Good luck! – Remzi Cavdar Nov 4 '18 at 0:07
1

Warning

Before you get started, I highly recommend you to create a child theme to safely modify the current theme you’re using. That way, if you need to update your theme, your changes won’t be overwritten.

Modifying your current error page

If your theme already has a custom error page, look for a 404.php file in your theme's root directory (/wp-content/themes/your-theme/404.php) and copy it into your child theme’s folder under (/wp-content/themes/your-theme-child/404.php). Modify this to your liking.

Creating an error page

If your theme doesn’t have a 404 error page set up, you could create one, but instead of creating an error page from scratch, you could copy your theme’s page.php file to your child theme directory and rename it 404.php (/wp-content/themes/your-theme-child/404.php) and modify this to your liking.

If you want to cover any other types of errors / HTTP Status Codes repeat the steps above and just rename the file to reflect the type of error page you want to create such as 404.php or 403.php.

See a list of HTTP Status Codes:

If you are unable to create a child theme or don't want to create one, you could always use a plugin: https://wordpress.org/plugins/404page/

Sources:


NGINX
Nginx is not my expertise, I use Apache. So bear with me:

# define error page
error_page 404 = @notfound;

# error page location redirect 302
location @notfound {
    return 404 /custom-error-page;
}

In your php block put the fastcgi_intercept_errors set to on

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    # intercept errors for 404 redirect
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

See for more information: https://guides.wp-bullet.com/nginx-redirect-404-errors-to-homepage-wordpress/

  • the idea is to circumvent Wordpress and handle errors at the Nginx Server level... Is this do-able ? – Joanna Mikalai Nov 4 '18 at 11:04
  • Everything is routed to index.php for WP to work correctly, however, you're able to define in WordPress an error page or exclude processing to index.php or as way say bypass this default behaviour – Remzi Cavdar Nov 4 '18 at 11:29
  • I have updated my answer to include an option to bypass WordPress default error handling, see my answer. – Remzi Cavdar Nov 4 '18 at 11:40
  • @JoannaMikalai Could you look at my edited answer? Did you solve your issue? – Remzi Cavdar Nov 5 '18 at 8:54
  • Just adding "fastcgi_intercept_errors on;" solved the issue, although I am not sure if I have to make any other changes to my setup.. – Joanna Mikalai Nov 5 '18 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.