I would like to to route requests based on a path to two different Angular applications. So when i request http://example.com/admin is routes to one app http://example.com/client routes to the second app. I have the following config but all requests are always sent to the nginx default page. Configuration is as follows:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location /admin {
root /home/ubuntu/apps/admin/;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location /client {
root /home/ubuntu/apps/client;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
}
No other confs are in /etc/nginx/sites-enabled
and nginx.conf is default post install on Ubuntu. Any help is appreciated.
2
Answers
It appears that you cannot use multiple
root
directives but instead need to usealias
(Configure nginx with multiple locations with different root folders on subdomain). With that, I would still get 404s until I took off$args
from theindex.html
. After that everything worked fine (don't ask how long it took to figure that out). Working config:You were using the wrong value for the
root
directive. In both locations the correct value for theroot
directive is/home/ubuntu/apps
, which means you can simplify the configuration by using just oneroot
directive by moving it into theserver
block.Of course you can use the
alias
directive – but as the manual states :The other problem is that your
try_files
statements are pointing to the wrongindex.html
file.For example:
Note that
server_name _;
is not necessary – see the Server Names document.Also,
index index.html;
is not necessary being the default value for theindex
directive.