英文:
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; }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论