Configure nginx with multiple locations with different root folders on subdomain
You need to use the alias
directive for location /static
:
server { index index.html; server_name test.example.com; root /web/test.example.com/www; location /static/ { alias /web/test.example.com/static/; }}
The nginx wiki explains the difference between root and alias better than I can:
Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.
Note that root
and alias
handle trailing slashes differently.
The Location directive system is
Like you want to forward all request which start /static
and your data present in /var/www/static
So a simple method is separated last folder from full path , that means
Full path : /var/www/static
Last Path : /static
and First path : /var/www
location <lastPath> { root <FirstPath>;}
So lets see what you did mistake and what is your solutions
Your Mistake :
location /static { root /web/test.example.com/static;}
Your Solutions :
location /static { root /web/test.example.com;}
server { index index.html index.htm; server_name test.example.com; location / { root /web/test.example.com/www; } location /static { root /web/test.example.com; }}