Regex nginx:根据路径选择根

Regex nginx:根据路径选择根,regex,nginx,url-rewriting,root,directive,Regex,Nginx,Url Rewriting,Root,Directive,我想要下面的。 应该访问/home/person1/some/path(和访问/home/person1/index.html),应该访问/home/person2/some/path(和访问/home/person2/index.html)。将会有很多人。使用正则表达式告诉nginx在哪里查找所有内容是很重要的 我试着想出一套可行的位置、根和重写指令。最接近我的是我的可用站点/网站 server { listen 80 default_server; liste

我想要下面的。 应该访问
/home/person1/some/path
(和访问
/home/person1/index.html
),应该访问
/home/person2/some/path
(和访问
/home/person2/index.html
)。将会有很多人。使用正则表达式告诉nginx在哪里查找所有内容是很重要的

我试着想出一套可行的位置、根和重写指令。最接近我的是我的
可用站点/网站

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        root /some/default/root;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri.html $uri $uri/ =404;
        }

        location /person1 {
                root /home/person1;
                rewrite ^/person1(.*)$ $1 break;
        }
}

除了表单中的路径之外,我希望所有路径都是这样。在这种情况下,nginx不会像我希望的那样访问
/home/person1/index.html
。相反,regex返回一个nginx不喜欢的空字符串(我在nginx/error.log中看到了抱怨)。

当您在/home中有公共起始根目录时,您可以尝试:

location ~* /person\d+ {
    root /home;
}