繁体   English   中英

nginx 路由做不同的路径与角度不工作

[英]nginx routing do diffrent path with angular are not working

大家好,我的 Kubernetes 集群中的 angular 项目路由有问题。 我解释了我是如何进行的我希望我的 url 中的起始根目录不是 / 但 /login 一切都应该以 /login 开头,这是 Angular 应用程序,但是一旦我在 nginx 中使用 docker 加载它并将其放入 Kubernetes集群和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

谢谢你的帮助。

你可以试试 ingress 中的注解

traefik.ingress.kubernetes.io/rewrite-target: /login

例子

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/rewrite-target: /login
    traefik.ingress.kubernetes.io/router.tls: "true"
    #traefik.ingress.kubernetes.io/router.entrypoints: websecure
    cert-manager.io/cluster-issuer: daimler-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

他每个人的解决方案都在 angular 应用程序 angular.json a basheref

          "configurations": {
            "production": {
              "baseHref": "/login/", 
........

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM