简体   繁体   English

nginx:根基于路径选择

[英]nginx: root chosen based on path

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 ). 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 )。 There will be many personX es. 将会有很多personX It's important to use a regular expression to tell nginx where to find everything. 使用正则表达式告诉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 . 我最接近的是我的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 . 这对所有路径都符合我的要求,除了http://some.site/person1形式的路径之外。 In this case, nginx doesn't access /home/person1/index.html like I want. 在这种情况下,nginx不会像我想要的那样访问/home/person1/index.html Instead, the regex returns an empty string which nginx doesn't like (I see complaints in the nginx/error.log). 相反,正则表达式会返回nginx不喜欢的空字符串(我在nginx / error.log中看到了抱怨)。

when you have common start root dir in /home, you can try with: 当您在/ home中具有通用的启动根目录时,可以尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM