简体   繁体   English

在 Nginx Ingress AKS 上设置 Angular 应用程序

[英]Setup Angular application on Nginx Ingress AKS

I deployed an Angular SPA application in Azure Kubernetes.我在 Azure Kubernetes 中部署了一个 Angular SPA 应用程序。 I was trying to access the application through the Ingress Nginx controller.我试图通过 Ingress Nginx 控制器访问应用程序。 The Ingress is pointing to xxx.xxx.com and the app is deployed in the path "/". Ingress 指向 xxx.xxx.com,应用部署在路径“/”中。 So when I accessed the application the application is loading fine.因此,当我访问该应用程序时,该应用程序加载良好。 But when I try to navigate to any other page other than index.html by entering it directly in the browser (ex: eee.com/homepage), I get 404 Not Found.但是,当我尝试通过直接在浏览器中输入 index.html 来导航到除 index.html 之外的任何其他页面时(例如:eee.com/homepage),我得到 404 Not Found。

Following is the dockerfile content以下是dockerfile内容

# base image
FROM nginx:1.16.0-alpine
# copy artifact build from the 'build environment'
COPY ./app/ /usr/share/nginx/html/
RUN rm -f /etc/nginx/conf.d/nginx.config
COPY ./nginx.config /etc/nginx/conf.d/nginx.config
# expose port 80
EXPOSE 80
# run nginx
CMD ["nginx", "-g", "daemon off;"]

Ingress.yaml入口.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-frontend-ingress
  namespace: app
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - xxx.com
    secretName: tls-dev-app-ingress
  rules:
  - host: xxx.com
    http:
      paths:
      - backend:
          serviceName: poc-service
          servicePort: 80
        path: /

nginx.conf配置文件

server {

  listen 80;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  error_page   500 502 503 504  /50x.html;

  location = /50x.html {
    root   /usr/share/nginx/html;
  }

}

You can add a configuration-snippet that will take care of rewriting any path to / :您可以添加一个配置片段,负责重写/任何路径:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-frontend-ingress
  namespace: app
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"    
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite /([^.]+)$ / break;
      [...]

For example, if you go to host.com/xyz it will be redirected to host.com例如,如果您访问host.com/xyz ,它将被重定向到host.com

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

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