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?

1 Answer 1

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;
    }
}
1
  • 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 example.com/app2_subpage/somepage or even to example.com/app2_subpage, it seems to fall under the "location /" block and redirects me to the app1 page.
    – bigfan
    Feb 3, 2021 at 8:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .