【发布时间】:2021-12-27 19:44:42
【问题描述】:
大家好,我的 Kubernetes 集群中的 Angular 项目路由存在问题。我解释了我如何继续我希望我在 url 中的起始根不是 / 但 /login 一切都应该以 /login 开头,这就是 Angular 应用程序,但是一旦我在 nginx 中使用 docker 加载它并将其放入 Kubernetes cluster 和 tearafik ingress 将 url 路由更改为 /login 而不是 / 在 web 控制台中发生以下错误:
efused to apply style from 'https://XXXXXXt/styles.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
login:14 GET XXXXXXXXXXX/runtime.js net::ERR_ABORTED 404
login:14 GET XXXXXXXXXXX/polyfills.js net::ERR_ABORTED 404
login:14 GET XXXXXXXXXXX/scripts.js net::ERR_ABORTED 404
login:14 GET XXXXXXXXXXXet/main.js net::ERR_ABORTED 404
我解释我如何进行 在 Angular 中,我正在路由所有内容:
{
path: '', component: DefaultComponent,
},
{
path: 'conformity-report',
loadChildren: () => import('./all-products/conformity-report/conformity-report.module').then(m => m.ConformityReportModule)
},
{
path: 'user-management', canActivate: [AdminGuard],
loadChildren: () => import('./all-products/user-management/user-management.module').then(m => m.UserManagementModule)
},
{
path: 'configuration-area', canActivate: [AdminGuard],
loadChildren: () => import('./all-products/configuration-area/configuration-area.module').then(m => m.ConfigurationAreaModule)
},
{
path: 'admin', canActivate: [AdminGuard],
loadChildren: () => import('./all-products/admin/admin.module').then(m => m.AdminModule)
},
{
path: 'error-page',
component: ErrorPageComponent
},
{ path: '**', redirectTo: '' }
];
这是我的 nginx 配置文件
server {
listen 8080;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
try_files $uri $uri/ /index.html;
include /etc/nginx/mime.types;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
}
这是我的 dockerfile,我将编译后的应用程序与配置文件一起移动到 nginx
FROM XXXXXXX/nodejs:14.17 as build
ENV XXXXXXXX
WORKDIR /app
COPY package*.json /app/
COPY . .
RUN npm install
RUN npm run build
FROM XXXXXXX/nginx:unpriviliged
EXPOSE 8080:8080
COPY --from=build /app/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist/projectname /usr/share/nginx/html
我的部署和服务文件工作正常,但我将在此处传递我的 Ingress 文件我们正在使用 taerafik
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/router.tls: "true"
#traefik.ingress.kubernetes.io/router.entrypoints: websecure
cert-manager.io/cluster-issuer:XXXXXX-acme
name: ingresshosts
namespace: dev
spec:
rules:
- host: XXXXXXXX
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend
port:
number: 8080
tls:
- hosts:
- XXXXXXXX
secretName: XXXXXXXX
感谢您的帮助。
【问题讨论】:
-
在下面提出的注释解决方案之上进行以下更改:删除 / 位置中的包含(它已经存在于父上下文中),其他两个位置没有需要(当从浏览器中的文件(如 js 和 CSS 文件)中检索文件时,不需要路由,因为它路由到文件路径并且无论如何都不使用)
-
我现在添加/登录了,还是不行
-
我现在添加/登录,它仍然无法工作'''apiVersion:networking.k8s.io/v1 kind:入口元数据:注释:kubernetes.io/ingress.class:traefik traefik.ingress .kubernetes.io/router.tls: "true" #traefik.ingress.kubernetes.io/router.entrypoints: websecure traefik.ingress.kubernetes.io/rewrite-target: /login raefik.ingress.kubernetes.io/app- root: "/login" cert-manager.io/cluster-issuer: xxxxx-acme traefik.ingress.kubernetes.io/rule-type: "PathPrefixStrip" 规则: http: 路径: - 路径: /login
标签: angular docker nginx kubernetes kubernetes-ingress