0

I recently made a custom 404 page for my WordPress blog running on Nginx. However, the page lives above my root directory. I thought I set the parameters correctly, but it doesn't seem to be working (I still get the 404 page from my theme). Am I missing something simple? I'd appreciate any help.

...
...
...
root /var/www/wordpress/sitenamehere;           #Set document root
autoindex off;                                  #Turn off index browsing everywhere
index index.php index.html;                     #Set indexes to include .php before .html

#Error pages
error_page 404 /404.php;
location /404.php {
  root /var/www/custom_404/404.php;
  internal;
}

location / {
try_files $uri $uri/ /index.php?$args;
...
...
...
 location ~* \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_index index.php;
    include fastcgi_params;
    }
...
...
...

Thanks!

Logan M.
  • 11
  • 3
  • What does the nginx `error.log` show? What is the PHP configuration in nginx? – Tero Kilkanen Aug 22 '16 at 20:16
  • The error.log has nothing relevant. The access.log file is here, and it shows the 404 code being throw. `XX.XX.XX.XX - - [22/Aug/2016:16:25:26 -0400] "GET /asdfasdf HTTP/1.1" 404 10397 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"`. I added the PHP section to the original post. – Logan M. Aug 22 '16 at 20:26

1 Answers1

0

I think the issue here is that you need to add the same fastcgi directives to the location /404.php block as there are in the location ~ * \.php$ block. nginx processes only one location block, and it has no information on how to actually execute the PHP file once inside the location /404.php block.

The root directive is also wrong, it has to always contain a directory, which is used as the base where the URI is appended to find the resource. In your case this configuration should work:

location /404.php {
    root /var/www/custom_404;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    include fastcgi_params;
}
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • I tried that, but I'm still getting the regular 404 error. However, I do see your point about the root and the PHP processor. – Logan M. Aug 23 '16 at 00:53