0

We're hosting 2 Angular applications on one Linux server running nginx. We'd like to have use same hostname within 2 Angular apps. Let's say when I browse to http://example.com, it'll use one of these apps, but if I browse to a specific page, it'd use the other one. I can't seem to get this to work.

So we have http://example.com (app1) that serves dynamic content
and
we have http://example.com/app2 and http://example.com/app2_subpage that are used by the app2 and also serve dynamic content

Both of these apps are meant to run together as they are different services of one whole system, that's why we want to run them under same machine/DNS.

nginx conf:

server {
        listen 80;
        server_name example.com;

        location ~ ^/(app2|app2_subpage)/ {

                root /path/to/app2;
                try_files $uri$args $uri$args/ /index.html;
        }

        location / {
                root /path/to/app1;
                try_files $uri$args $uri$args/ /index.html;
        }
}

So the http://example.com(app1) works with all its dynamic content but http://example.com/app2 and http://example.com/app2_subpage gets redirected back to http://example.com(app1).

It seems that the regex for the app2 location block isn't correct. What would be the configuration in this case?

bigfan
  • 1

1 Answers1

0

You are trying to load http://example.com/app2, but the regular expression matches only http://example.com/app2/.

Try the following configuration:

server {
    listen 80;
    server_name example.com;

    location ~ ^/(?:app2|app2_subpage) {
        root /path/to/app2;
        try_files $uri$args $uri$args/ /index.html;
    }

    location / {
        root /path/to/app1;
        try_files $uri$args $uri$args/ /index.html;
    }
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Hi Thanks for the follow-up! Unfortunately this didn't work either. For some reason, this regex fails and I can't figure out why. For example, when I browse to http://example.com/app2_subpage/somepage or even to http://example.com/app2_subpage, it seems to fall under the "location /" block and redirects me to the app1 page. – bigfan Feb 03 '21 at 08:09