Nginx – Can’t get 404s for WordPress pages with permalinks and nginx

http-status-code-404nginxpermalinksWordpress

I have nginx 1.6.2 serving a wordpress site, with php-fpm.

Permalinks are setup the following way: /blog/%postname%/

nginx serves correctly the site pages (/, /products, /contact…) as well as the blog articles (under/blog/). 404s work well for blog articles (i.e. example.com/blog/idontExist will respond with 404). However, browsing to example.com/iDontExist gets HTTP 200 reponse and the content of the site's index page, instead of the 404.

Here is my nginx.conf:

server {
    server_name example.com;

    root /var/local/example.com/;

    access_log /var/log/nginx/example.com-access.log main;
    error_log  /var/log/nginx/example.com-error.log;

    error_page 404 /index.php?$request_uri;

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # fastcgi_split_path_info ^(/blog)(/.*)$;
        include /etc/nginx/fastcgi_params;
        #fastcgi_intercept_errors on;
        #fastcgi_param  PATH_TRANSLATED         $document_root$fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index /index.php;

    }

    location / {
        auth_basic           "private section";
        auth_basic_user_file /var/local/example.com/conf/.htpasswd;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ /\.ht {
        deny  all;
    }
}

and here are my fastcgi_params:

fastcgi_param  QUERY_STRING   $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE   $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI   $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# bug workaround : http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

I've read and tried many variations based on what's on forums, but nothing worked.

Best Answer

turns out the issue got fixed by editing php.ini:

cgi.fix_pathinfo=0

replaced by

cgi.fix_pathinfo=1

then reloading the php-fpm daemon.