How to configure 404s when using LEMP (Nginx) and WordPress

September 6, 2013 8.5k views
With my WordPress sites on DO VPS, when a 404 is reached the nginx 404 page is displayed rather than the WordPress one I would expect. All the results in Google are quite old and relate to using permalinks and then the site 404ing. I am delighted to say that WordPress permalinks are working fine with my sites so its not connected to that. Any suggestions?
6 Answers
Weird. Does WP act normal in all other respects? If so, then the only thing that I can think of is...

If you're running Ubuntu, look in /etc/nginx/sites-enabled Then, execute cat [yoursite]

Do you have a line that reads error_page 404 /404.html;? If so, change the location to your WordPress directory that contains the 404 page you want.
Ahhh, thank you, I think you may have hit the nail on the head. From my my sites-enabled (sites-available):

# deliver a static 404
error_page 404 /404.html;
location /404.html {

What exactly do I need to change here? With usual WP behaviour (permalinks enabled), I can make WP display the error page when I deliberately mistype a url. For example

WP then displays the 404 template file (in most cases I show a not found message / search / popular links etc.)
Upon a 404 error, error_page 404 /404.html; is telling the web server to look for the file named 404.html in the directory referenced in the line that begins with root ....

If you kept the default Nginx root directory, it should be root /usr/share/nginx/www;.

Take a look at the /usr/share/nginx/www/ directory. Is there a file named 404.html in there?

If not, either drop one in or change the error_page 404 ... line to something like error_page 404 /wp-content/themes/genesis/404.php
Did you follow a specific article on installing Wordpress? If so, can you please post it?

Check nginx's error logs, do you see anything?

tail /var/log/nginx/error.log
Hi thanks both of you for your responses. @Pablo I didn't want to have to explicitly set each 404 for my sites incase the theme changed.

I realised I was explicitly requesting Nginx to handle the 404 pages rather than let WordPress handle them. I had the following set in my conf for each site in sites-enabled.

fastcgi_intercept_errors on;

Setting this to off (+ removing the redundant 'location' for the 404) resolved this issue for me. Thanks again both.
nano /usr/local/apps/nginx/etc/conf.d/webuzoVH.conf

server {
listen          80;
# The Document Root
root            /home/user/public_html;
error_log       /usr/local/apps/nginx/var/log/;
access_log      /usr/local/apps/nginx/var/log/ main;
include         /usr/local/apps/nginx/etc/conf.d/common;
try_files $uri $uri/ /index.php?q=$uri&$args;
Have another answer? Share your knowledge.