nginx:根据路径选择根



我想要以下内容。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)。会有很多personX。使用正则表达式告诉nginx在哪里可以找到所有内容非常重要。

我尝试提出一组可以工作的位置、根和重写指令。我最接近的是我的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;
        }
}

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

当你在/home 中有共同的起始根目录时,你可以尝试:

location ~* /persond+ {
    root /home;
}

最新更新