Nginx位置正则表达式来处理/ sub /目录中多个WordPress站点的永久链接

问题描述 投票:1回答:1

我正在寻找一个动态的解决方案来处理安装在子目录中的多个WordPress站点(节点)的相当永久链接。

可以使用这样的URL访问这些站点(我使用clusternode来表示结构,但它们对于每种情况都不同,但它们始终遵循相同的结构,节点是包含WordPress根文件的目录):

https://www.domain.tld/cluster1/node1/

而我想避免的是每个节点创建一个规则,如下所示:

location /cluster1/node1/ {
    try_files $uri $uri/ /cluster1/node1/index.php$is_args$args;
}

location /cluster2/node2/ {
    try_files $uri $uri/ /cluster2/node2/index.php$is_args$args;
}

location /cluster3/node3/ {
    try_files $uri $uri/ /cluster3/node3/index.php$is_args$args;
}

location /cluster4/node4/ {
    try_files $uri $uri/ /cluster4/node4/index.php$is_args$args;
}

这是有效的,但有超过43个节点(不断变化)。所以,我尝试了以下内容:

location /([^/]+)/([^/]+)/ {
    try_files $uri $uri/ /$1/$2/index.php$is_args$args;
}

正确显示主页,但显示节点页面的404(如https://www.domain.tld/cluster1/node1/page/)(由Nginx呈现而不是WordPress)。

location ~ ^/([^/]+)/([^/]+)/ {
    try_files $uri $uri/ /$1/$2/index.php$is_args$args;
}

但这使得PHP文件被下载为名为download的文件。

location ~ /([^/]+)/([^/]+)/ {
    try_files $uri $uri/ /$1/$2/index.php$is_args$args;
}

和以前一样。

location ^~ /([^/]+)/([^/]+)/ {
    try_files $uri $uri/ /$1/$2/index.php$is_args$args;
}

这使得该节点的主页下载与之前的尝试相同,但显示节点页面的404(如https://www.domain.tld/cluster1/node1/page/)(由Nginx呈现而不是WordPress)。

任何以上都不起作用的任何线索?有关如何使其工作的任何建议?

感谢大家!

regex wordpress nginx nginx-location nginx-config
1个回答
1
投票

您需要使用正则表达式location块来捕获内部重定向到正确的index.php处理程序的参数。使用location~修饰符声明正则表达式~*。有关详细信息,请参阅this document

正则表达式按顺序进行计算,因此location\.php$必须放在您插入的location之上。否则,将下载PHP文件而不是执行。

例如:

location ~ \.php$ {
    ...
}
location ~ ^/([^/]+)/([^/]+)/ {
    try_files $uri $uri/ /$1/$2/index.php$is_args$args;
}
© www.soinside.com 2019 - 2024. All rights reserved.