简体   繁体   English

子目录中的 WordPress 在 nginx fastcgi 上提供 root index.php

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

I am encountering a weird problem on my NGINX server.我在我的 NGINX 服务器上遇到了一个奇怪的问题。

As explained in the Codex I moved wordpress from my root directory to a subdirectory /blog/.如 Codex 中所述,我将 wordpress 从我的根目录移动到子目录 /blog/。

It shows the blog index successfully, but if I want to show anything else, like a specific post or archive page it serves the root index.php它成功显示了博客索引,但是如果我想显示其他任何内容,例如特定的帖子或存档页面,它会为根 index.php 提供服务

Even non-existent URLs are served as the root index.php甚至不存在的 URL 也被用作根 index.php

If I remove the root index.php it returns an 404 error.如果我删除根 index.php,它会返回 404 错误。

Maybe its due to my nginx and fastcgi settings, but I really have no clue:也许是因为我的 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
    }

}

Edit: Updated with whole Server Block编辑:更新整个服务器块

The try_files statement is sending all none-existent files to index.php in your web root. try_files 语句将所有不存在的文件发送到您的 web 根目录中的 index.php。 Add a second location specifically for your blog to the configuration:将专门用于您的博客的第二个位置添加到配置中:

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

try_files works as follows: try_files 的工作原理如下:

Checks the existence of files in the specified order and uses the first found file for request processing;按指定顺序检查文件是否存在,并使用第一个找到的文件进行请求处理; the processing is performed in the current context.处理是在当前上下文中执行的。 The path to a file is constructed from the file parameter according to the root and alias directives.文件的路径是根据 root 和 alias 指令从 file 参数构造的。 It is possible to check directory's existence by specifying a slash at the end of a name, eg “$uri/”.可以通过在名称末尾指定斜杠来检查目录是否存在,例如“$uri/”。 If none of the files were found, an internal redirect to the uri specified in the last parameter is made.如果没有找到任何文件,则内部重定向到最后一个参数中指定的 uri。 For example:例如:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM