Nginx dynamic location configuration

2019-07-25 13:15发布

问题:

My goal is to configure nginx to match locations dynamically like this:

http://www.domain.com/app-one
http://www.domain.com/app-two
http://www.domain.com/app-three

/usr/share/nginx/html/app-one/public
/usr/share/nginx/html/app-two/public
/usr/share/nginx/html/app-three/public

I'm confused how to do this. And if I can do it like this or if it's recommended or best practice to do something like this (I don't want to go the subdomain solution).

If I'm trying this:

location ~ /(^/)+ {
    alias   /usr/share/nginx/html/$1/public;
    index  index.html;
}

All I get is an 403.

But there's a way to handle that, right? Do you know how?

PS. Neither I found any explanation via google nor here that helps me.

回答1:

Try this

location ~ ^/([^/]+)(.*)$ {
    alias   /usr/share/nginx/html/$1/public$2;
    index  index.html;
}


标签: nginx