Nginx位置指令中子目录的正则表达式或通配符 [英] Regex or wildcard for subdirectories in an Nginx location directive

查看:833
本文介绍了Nginx位置指令中子目录的正则表达式或通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developers who will be working on their local machines editing multiple Wordpress sites. I'd like to set up Nginx for them one time without the need for them to edit the config file in the future. Typically when Nginx is configured to host Wordpress, a location block such as this is included:

location / {
try_files $uri $uri/ /index.php$is_args$args;
} # End location

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

In our situation, each WP site will be in its own subdirectory. So when a developer needs to view a site, they'll go to a URL in their browser such as:

http://localhost/site1
http://localhost/site2
http://localhost/site3

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

What we would like is for the location directive above to include the subdirectories. As it is now, it only includes the root (http://localhost) and not the subs. I think this requires a wildcard or regex of some kind, but I'm not sure.

换句话说,我想我正在寻找一个位置块,例如:

In other words, I think I'm looking for a location block like:

location /all-subdirectories {
try_files $uri $uri/ /whatever-subdirectory/index.php$is_args$args;
} # End location

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

Does this make sense or am I on the wrong track?

推荐答案

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

You could use a regular expression location to capture the first part of the URI, for example:

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

或使用具有一个或多个rewrite语句的命名位置,例如:

Or use a named location with one or more rewrite statements, for example:

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

这篇关于Nginx位置指令中子目录的正则表达式或通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆