WordPress子目录在nginx fastcgi上提供根索引.php


WordPress in sub-directory is serving root index.php on nginx fastcgi

我的NGINX服务器遇到了一个奇怪的问题。

正如Codex中所解释的那样,我将wordpress从根目录移到了子目录/blog/。

它成功地显示了博客索引,但如果我想显示其他内容,比如特定的文章或存档页面,它会提供根索引。

甚至不存在的url也被作为根索引。

如果我删除根index.php,它会返回404错误。

可能是由于我的nginx和fastcgi设置,但我真的没有线索:

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index index.php index.html index.htm;
    client_max_body_size 10M;
    # Make site accessible from http://localhost/
    server_name

            set $no_cache 0;
            if ($request_method = POST){set $no_cache 1;}
            if ($query_string != ""){set $no_cache 1;}
            if ($http_cookie = "PHPSESSID"){set $no_cache 1;}
            if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {set $no_cache 1;}
            if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in"){set $no_cache 1;}

    location ~ '.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+'.php)(/.+)$;
            fastcgi_cache  microcache;
            fastcgi_cache_key $scheme$host$request_uri$request_method;
            fastcgi_cache_valid 200 301 302 30s;
            fastcgi_cache_use_stale updating error timeout invalid_header http_500;
            fastcgi_pass_header Set-Cookie;
            fastcgi_no_cache $no_cache;
            fastcgi_cache_bypass $no_cache;
            fastcgi_pass_header Cookie;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
}

编辑:更新整个Server Block

try_files语句将所有不存在的文件发送到web根目录下的index.php。在配置中专门为您的博客添加第二个位置:

location /blog {
    try_files $uri $uri/ /blog/index.php;
}

try_files的工作方式如下:

按指定顺序检查文件是否存在,并使用首先找到文件进行请求处理;执行处理在当前上下文中。构造文件的路径根据root和alias指令设置文件参数。它是指定斜杠可以检查目录是否存在名称的结尾,例如"$uri/"。如果没有找到任何文件,那么对最后一个参数中指定的uri进行内部重定向。例如: