3

I configured my WordPress website on Nginx and almost everything works as expected. I get the WordPress 404 for every non-existent page as expected -- except for non-existent .php pages! Those return the nginx default 404 page not found. Why is this? I would like to get the WordPress 404.

Thanks! This is the config file:

set $skip_cache 0;

if ($request_method = POST) {
    set $skip_cache 1;
}

if ($query_string != "") {
    set $skip_cache 1;
}

if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
    set $skip_cache 1;
}

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}

location ~ /purge(/.*) {
    fastcgi_cache_purge MYWP "$scheme$request_method$host$1";
}

location @php {
    try_files $uri =404;
    add_header X-Cache $upstream_cache_status;
    include /etc/nginx/fastcgi_params;
    ####
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    fastcgi_cache latigreelacrobata.lanavediteseo.eu;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache_valid 200 60m;
    fastcgi_intercept_errors on;

    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_read_timeout 240;
}

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~* ^.+\.(css|js|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; log_not_found off; expires max;
}
Jesse Nickles
  • 250
  • 1
  • 12
user103969
  • 31
  • 2
  • Hi, thanks for edits and for the answer. I solved it changing the php part from try_files $uri =404 to try_files $uri $uri/ /index.php?$args; }. I hope this is not an insecure edit, what do you think? – user103969 Oct 03 '16 at 14:06
  • Related: https://serverfault.com/questions/751314/nginx-serve-plain-file-if-exists-else-serve-index-php – Jesse Nickles Sep 10 '22 at 19:12

2 Answers2

2

You have the following lines in your configuration:

location @php {
    try_files $uri =404;

Its says that, if a php file is not found, nginx will throw its own 404 page.

If you want Wordpress to deal with 404 also in those cases, replace it by:

location @php {
    try_files $uri $uri/ /index.php?$args;

This will direct the request to Wordpress, and it will show your desired 404 page.

Alan Tygel
  • 21
  • 2
  • This is great for usability and easy management, however, keep in mind Nginx will now send absolutely any non-existent file requests to WordPress, meaning it could potentially overwhelm your server or make i.e. DDOS attacks easier. – Jesse Nickles Sep 10 '22 at 19:04
0

I can't say what you have is definitely incorrect, but I can say your PHP section is quite different to my working PHP sections. Here's the core of how I do it.

location ~ \.php$ {
  fastcgi_keep_conn on;

  fastcgi_pass   php56-fpm;
  include        fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

I have a tutorial on Nginx/Wordpress which includes full configuration files you can download, here. It goes into caching and all kinds of things.

Tim
  • 30,383
  • 6
  • 47
  • 77