1

I was wondering how to serve an angular app based on the url while using nginx. So let's say there is a url called xyz.com/something/ and whenever there's something in the url, I want ngnix to route to an angular app. How do we acheive it, I saw similar questions here on SO like this but just couldn't get it working.

location /something/ {
    root /var/www/xyz.com/html/something/;
    try_files $uri $uri/ /index.html =404;
    }

let's say normally the app would respond to urls like, xyz.com/something/login

The below code is working fine though.

location / {
  alias /var/www/xyz.com/html/something/;
  try_files $uri $uri/ /index.html =404;
}

Have tried something with base-href too as described here and yet failed to get it working.

Tried the first answer as well.

Nginx CONFIG file:

location /admin {
    root /var/www/xyz.com/html/;
    try_files $uri $uri/ /admin/index.html;
}

location / {
    alias /var/www/xyz.com/html/user/;
    try_files $uri $uri/ /index.html =404;
}

Here /admin & /user refers to two different AngularJs projects present on same domain.

TEST CASES:

URL : www.xyz.com/admin/
RESPONSE : Blank page.. 
           accessing index.html but not redirecting properly

URL : www.xyz.com/admin/login
RESPONSE : Blank page..
  ERRORS : Cannot match any routes. URL Segment: 'login'

but I have routed properly..

Any Suggestions??

Nobody
  • 397
  • 1
  • 5
  • 25

2 Answers2

0

You have too many "something"s. The path to the file is constructed by appending the URI to the value of root. See this document for details.

In your configuration, the file terms $uri and $uri/ will not match andy files.

If you remove the something from your root value, you will need to add it to the index.html file element.

For example:

location /something/ {
    root /var/www/xyz.com/html;
    try_files $uri $uri/ /something/index.html;
}

Personally, I do not append the =404, but it does no harm, and I know that many Angular tutorials seem to think it's necessary. See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Hey, tried your suggestion as well. Have updated the results from it on the question above. Kindly have a look at it. – Nobody Dec 19 '18 at 15:37
0
location /angular/ {
    root   /opt/app-root/src;
    index  index.html index.htm;
    try_files $uri $uri/ /angular/index.html;
}
COPY dist/angular-app/. /opt/app-root/src/angular
fcdt
  • 2,371
  • 5
  • 14
  • 26