You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地K8s部署Angular+NestJS应用遇Ingress Nginx 500无日志报错

问题:Kubernetes Ingress部署Angular+NestJS应用出现500错误

我正尝试在本地Windows机器上,使用Docker和Kubernetes Ingress部署Angular前端、NestJS后端的应用。本地环境下应用运行正常,推送至Docker Hub后的镜像也能正常运行,但部署到Kubernetes后访问始终出现500错误。

注:Ingress控制器未显示任何错误信息!

后端部署配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: smartagri-back 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: smartagri-back
  template:
    metadata:
      labels:
        app: smartagri-back
    spec:
      containers:
      - name: smartagri-back
        image: maryem1994/smartagri:back-3
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: smartagri-back
spec:
  type: ClusterIP
  ports:
  - port: 80
  selector:
    app: smartagri-back

前端部署配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: smartagri-front 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: smartagri-front
  template:
    metadata:
      labels:
        app: smartagri-front
    spec:
      containers:
      - name: smartagri-front
        image: maryem1994/smartagri:front-3
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: smartagri-front
spec:
  type: ClusterIP
  ports:
  - port: 80
  selector:
    app: smartagri-front

Nginx Ingress配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: smartagri-app.com
      http:
        paths:
          - path: /saback
            pathType: Prefix
            backend:
              service:
                name: smartagri-back
                port:
                  number: 80
          - path: /
            pathType: Prefix
            backend:
              service:
                name: smartagri-front
                port:
                  number: 80

排查建议

  • 检查Pod运行状态:执行kubectl get pods,确认两个应用的Pod处于Running状态,重启次数为0
  • 查看应用日志:分别执行kubectl logs <smartagri-back-pod-name>和kubectl logs <smartagri-front-pod-name>,排查应用自身是否抛出异常
  • 测试集群内服务连通性:启动临时测试Pod(kubectl run -it --rm busybox --image=busybox:1.28 -- sh),在其中访问http://smartagri-back和http://smartagri-front,确认服务能正常响应
  • 验证后端端口配置:确认NestJS应用实际监听的端口是80,Dockerfile中是否正确暴露端口,启动命令是否指定了正确端口
  • 检查前端API请求路径:Angular前端调用后端的接口路径是否匹配Ingress的/saback前缀,若前端原本请求/api开头的接口,需调整Ingrewrite规则或前端代理配置
  • 调整Ingrewrite规则:当前配置会将/saback/xxx重写为/xxx转发到后端,如果后端接口本身带有/saback前缀,需修改rewrite-target为/$2,同时将path改为/saback(/|$)(.*)

内容的提问来源于stack exchange,提问作者maryem neyli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:27:38