Nginx将不同子域重定向到Angular应用程序未找到页面。

huangapple go评论50阅读模式
英文:

Nginx redirects different subdomain to Angular app not found page

问题

我有一个类似于sub.example.com的域名,我可以通过添加另一个子域级别来使用它来提供应用程序,例如test.sub.example.com。它运行良好,我可以在app1.sub.example.com上提供一个应用程序,另一个在app2.sub.example.com上提供。

现在,我有一个Angular应用程序,我使用以下配置在app.sub.example.com上使用nginx提供服务:

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;
  }
}

它运行良好,但我发现的唯一问题是,如果我访问尚未配置的子域,比如app-test.sub.example.com,我会被重定向到Angular应用程序上的/not-found端点,即app-test.sub.example.com/not-found,我可以看到未找到的页面。

// 路由数组中的未找到路由配置
{ path: 'not-found', component: PageNotFoundComponent, title: 'Page Not Found' },
{ path: '**', redirectTo: 'not-found', pathMatch: 'full' }

我是否在Angular应用程序的nginx配置中漏掉了什么?
或者是与Angular上的路由有关的问题?

编辑:

实际上,所有请求似乎都被重定向到Angular应用程序,我尝试访问app-test.sub.example.com/,它将我带到了Angular应用程序的索引页面。

英文:

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.

答案1

得分: 1

通常会创建一个默认的服务器块来处理未配置的所有请求。如此处所述 https://linuxserversetup.com/.../default-server-block

然后使用占位符 _ 作为 server_name

在此示例中,在/var/www/default目录下有一个index.htm文件,然后将其返回。

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; }
}
英文:

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; }
}

huangapple
  • 本文由 发表于 2023年6月22日 19:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定