How do i add a custom error page for my nginx web site?



  • Nginx documentation for error page.

    This is a section from sites-enabled/wordpress.conf, that is included in the nginx.conf, your setup may vary.

    To create a custom error page. See a section of the wordpress.conf file below:

       # The following applies to every PHP file
       location ~ .php$ {
             # Ensure file really exists
                if (!-e $request_filename) {
                      return 404;
                }
                # Pass the request to your PHP-FPM backend
             fastcgi_pass 127.0.0.1:9000;   
             fastcgi_index index.php;
             fastcgi_intercept_errors on;
             fastcgi_param SCRIPT_FILENAME /home/wordpress/www$fastcgi_script_name;
             include fastcgi_params;
    
            error_page 403 /error403.html; 
            error_page 404 /error404.html;
       }
    

    Your location section may not be php based.

    Lines 14,15 show two error pages for error types 403 and 404. Our document root is /home/wordpress/www, so we created the error files there; error403.html and error404.html.

    We also had to add line 10, so it bypasses the wordpress error page.

    If you have allow, deny rules, you can add a new location section to allow all error pages, incase you want to add error pages for 403 errors, 502, etc.

    location / { 
            error_page 404 /error404.html; 
            allow 192.168.0.0/16; 
            deny all; 
        } 
    
    location ~ "^/error[0-9]{3}.html$" { 
           allow all; 
    } 
    

 

© Lightnetics 2019