为什么 nginx 不喜欢我的 $args?

Jon*_*han 4 nginx wordpress

我有一个位于 的 WordPress 站点mysite.com/blog,其文件位于/var/www/mysite/wordpress. 为什么,当我访问 时mysite.com/blog/test,如果我$args从 中删除,我的嵌套位置块只会被击中try_files

server {
    server_name mysite.com;

    location /blog {
        alias /var/www/mysite/wordpress;
        try_files $uri $uri/ /blog/index.php$args;

        location ~ \.php {
            # This is only reached if I remove $args from try_files
            return 401; # For testing purposes 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是日志中的相关错误:

"/usr/share/nginx/html/index.php" failed (2: No such file or directory)

Mic*_*ton 8

因为你忘记了和?之间。便利变量在必要时包含一个,当没有查询字符串时为空。index.php$args$is_args?

try_files $uri $uri/ /blog/index.php$is_args$args;
Run Code Online (Sandbox Code Playgroud)

当然,对于 WordPress,您根本不需要使用$args,因为它会在别处获取查询字符串:

try_files $uri $uri/ /blog/index.php;
Run Code Online (Sandbox Code Playgroud)

足够了。

看来您正在使用try_fileswithalias指令。这是nginx中长期存在的错误,请改用root指令。