nginx:根据路径选择的root

时间:2022-07-16 08:28:27

I want the following. http://some.site/person1/some/path should access /home/person1/some/path (and http://some.site/person1 accesses /home/person1/index.html) and http://some.site/person2/some/path should access /home/person2/some/path (and http://some.site/person2 accesses /home/person2/index.html). There will be many personXes. It's important to use a regular expression to tell nginx where to find everything.

我想要以下内容。 http://some.site/person1/some/path应该访问/ home / person1 / some / path(和http://some.site/person1访问/home/person1/index.html)和http:// some .site / person2 / some / path应该访问/ home / person2 / some / path(和http://some.site/person2访问/home/person2/index.html)。会有很多人。使用正则表达式告诉nginx在哪里找到所有内容非常重要。

I tried coming up with a set of location, root and rewrite directives that would work. The closest I came was this for my sites-available/website.

我尝试提出一组可行的位置,根和重写指令。我最接近的是我的网站 - 可用/网站。

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;
        }
}

This does what I want with all paths except for ones of the form http://some.site/person1. In this case, nginx doesn't access /home/person1/index.html like I want. Instead, the regex returns an empty string which nginx doesn't like (I see complaints in the nginx/error.log).

除了http://some.site/person1之外,所有路径都能实现我想要的功能。在这种情况下,nginx不会像我想要的那样访问/home/person1/index.html。相反,正则表达式返回一个nginx不喜欢的空字符串(我在nginx / error.log中看到了抱怨)。

1 个解决方案

#1


0  

when you have common start root dir in /home, you can try with:

当您在/ home中有共同的启动根目录时,您可以尝试:

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

#1


0  

when you have common start root dir in /home, you can try with:

当您在/ home中有共同的启动根目录时,您可以尝试:

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