Nginx 位置指令中子目录的正则表达式或通配符

Aar*_*ams 3 wordpress nginx

我的开发人员将在本地计算机上编辑多个 Wordpress 站点。我想为他们设置一次 Nginx,而无需他们将来编辑配置文件。通常,当 Nginx 配置为托管 Wordpress 时,会包含如下位置块:

location / {
try_files $uri $uri/ /index.php$is_args$args;
} # End location
Run Code Online (Sandbox Code Playgroud)

在我们的情况下,每个 WP 站点都将位于其自己的子目录中。因此,当开发人员需要查看网站时,他们会在浏览器中访问一个 URL,例如:

http://localhost/site1
http://localhost/site2
http://localhost/site3
Run Code Online (Sandbox Code Playgroud)

我们希望上面的位置指令包含子目录。现在,它只包含根目录 ( http://localhost ),不包含子目录。我认为这需要某种通配符或正则表达式,但我不确定。

换句话说,我想我正在寻找一个像这样的位置块:

location /all-subdirectories {
try_files $uri $uri/ /whatever-subdirectory/index.php$is_args$args;
} # End location
Run Code Online (Sandbox Code Playgroud)

这有意义还是我走错了路?

Ric*_*ith 6

您可以使用正则表达式 location来捕获 URI 的第一部分,例如:

location ~ ^(/[^/]+) {
    try_files $uri $uri/ $1/index.php?$args;
}
Run Code Online (Sandbox Code Playgroud)

或者将命名位置与一个或多个rewrite语句一起使用,例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^(/[^/]+) $1/index.php last;
}
Run Code Online (Sandbox Code Playgroud)