Need help in using a different root for a location in NGINX

时间:2018-08-22 13:51:49

标签: nginx directive

I have a site configuration pointing to /var/www for a domain (https://somedomain.com/) but I want a location /clientfolder to be redirected to /home/clientfolder/website/

My issue is whenever a request comes in, such as https://somedomain.com/clientfolder/index.html the daemon is looking for a file at /home/clientfolder/website/clientfolder/index.html

So I tried to use the rewrite directive:

location /clientfolder {
    rewrite ^/clientfolder/(.*)$ /$1 last;
    root /home/clientfolder/website
}

But now https://somedomain.com/clientfolder/index.html loads the file at /var/www/index.html

Is there another directive than rewrite that would allow me to strip the "clientfolder" from the file path if the /clientfolder location was matched?


Update: I managed to get this to work, but it feels totally wrong:

location /clientfolder/website {
    root /home;
}
location /clientfolder {
    rewrite ^/clientfolder/(.*)$ /clientfolder/website/$1;
}

1 个答案:

答案 0 :(得分:1)

如果以/clientfolder开头的URI位于/home/clientfolder/website中,则应使用alias指令。

例如:

location /clientfolder {
    alias /home/clientfolder/website;
}

有关详细信息,请参见this document

相关问题