Barry Hughes WordPress Developer

WordPress, Nginx and 404 pages for .php files

15 January 2018

Nginx all setup and correct? Check! WordPress installed and ready to go? Check! 404 Not Found pages all working as expected? Almost 😐.

If you’re finding that attempts to access some.site/this-page-doesnt-exist bring you to a nicely crafted, theme-generated and possibly witty 404 Not Found page but some.site/some-nonexistent-file.php (note that *.php file extension) takes you to a rather starker looking Nginx-generated 404 page, then you have a few options that include just living with it.

Assuming for consistency’s sake however that the only option you really want to shoot for is to have non-existent *.php files take you a WordPress 404 page then what you’re probably looking for is the error_page directive.

The basic idea here is to tell Nginx to have index.php (ie, your main WordPress bootstrap file) handle all requests that result in a 404. Adding this early within your site configuration file’s server {} block – I like to place it right after my listen directives – resolves the problem and ensures that any time there is a 404, we get the nicely formatted response we were hoping for.

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

    listen 443 ssl;
    listen [::]:443 ssl;

    error_page 404 /index.php;
    # ...rest of config ...
}

🖇 WordPress Dev | Lemp | Nginx | PHP