1

I have a domain name like sub.example.com and I can serve apps using it by adding another subdomain level, like test.sub.example.com. It works fine and I could serve multiple apps one on app1.sub.example.com and another on app2.sub.example.com.
Now I have an angular app that I've served using nginx on app.sub.example.com using the following configuration:

server {
  server_name app.sub.example.com;
  listen 80;
  root /var/www/app;
  index index.html index.htm;
  location / {
    try_files $uri $uri/ /index.html;
  }
}

It works fine and all, but the only problem I found is that if I hit a subdomain that isn't configured yet, like say app-test.sub.example.com I get redirected to my /not-found endpoint on the angular application i.e., app-test.sub.example.com/not-found and I can see the not found page.

  // not found route configuration in the routes array
  { path: 'not-found', component: PageNotFoundComponent, title: 'Page Not Found' },
  { path: '**', redirectTo: 'not-found', pathMatch: 'full' }

Am I missing something on the nginx configuration for the Angular application? Or is it something related to the routing on Angular? \

Edit:

Actually all requests seems to be redirected to the Angular application, I tried hitting app-test.sub.example.com/ and it got me to the index page on the Angular application.

Isu
  • 330
  • 1
  • 4
  • 16

1 Answers1

1

Usually, a default server block is created that handles all requests that are not configured. As described here https://linuxserversetup.com/.../default-server-block

A placeholder _ is then used as the server_name.

In this example, there is an index.htm file in /var/www/default which is then returned.

server {
  listen      80;
  listen      [::]:80;
  server_name _;

  root        /var/www/default/;
  index       index.htm;

  location / {
    try_files $uri $uri/ /index.htm;
  }

  location = /favicon.ico { access_log off; log_not_found off; }
}
tom
  • 9,550
  • 6
  • 30
  • 49
  • I checked the sites-available and found a default configuration, that I enabled using `ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/` then reloading nginx. Now if I hit same endpoints as earlier, I still get redirected to the Angular app, but if I refresh, I get the nginx Not Found page. – Isu Jun 22 '23 at 11:31