无法通过永久链接和 nginx 获取 Wordpress 页面的 404 错误

无法通过永久链接和 nginx 获取 Wordpress 页面的 404 错误

我有 nginx 1.6.2,使用 php-fpm 为 wordpress 网站提供服务。

永久链接设置方式如下:/blog/%postname%/

nginx 正确地提供站点页面(/、、/products... /contact)以及博客文章(在 下/blog/)。404 适用于博客文章(example.com/blog/idontExist即将响应 404)。但是,浏览到 会example.com/iDontExist获得 HTTP 200 响应和站点索引页的内容,而不是 404。

这是我的 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;
    }
}

这是我的 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;

我已阅读并尝试了论坛上发布的许多变体,但都没有任何效果。

答案1

事实证明,通过编辑 php.ini 可以修复该问题:

cgi.fix_pathinfo=0

取而代之

cgi.fix_pathinfo=1

然后重新加载 php-fpm 守护进程。

相关内容