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

Nginx Ingress Controller返回404,请求排查K8s配置问题

问题:Ingress-Nginx返回404但Service正常工作

我在WSL2的minikube环境中为Kubernetes Service配置Ingress,Service直接访问正常,但通过Ingress访问返回404。以下是我的配置和测试过程:

Deployment配置

apiVersion: apps/v1
kind: Deployment
metadata:
    name: {{.Chart.Name}}-backend
    labels:
        app: {{.Chart.Name}}-backend
spec:
    replicas: {{ .Values.replicas }}
    selector:
        matchLabels:
            service: backend
            app: {{.Chart.Name}}-backend
    template:
        metadata:
            labels:
                service: backend
                app: {{.Chart.Name}}-backend
        spec:  
            containers:
            -   name: kuberdemo-app
                image: {{ .Values.image }}
                ports:
                -   containerPort: 8091
                    targetPort: 8091
                    protocol: TCP
                    name: app-port
                envFrom:
                - configMapRef:
                    name: kuberdemo-config
                startupProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                readinessProbe:
                    httpGet:
                        path: /actuator/health/readiness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                livenessProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                resources:
                    limits:
                        cpu: "2000m"
                        memory: "2Gi"
                    requests:
                        cpu: "2000m"
                        memory: "2Gi"

Service配置

apiVersion: v1
kind: Service
metadata:
  name: kuberdemo-service
spec:
  type: ClusterIP
  selector:
    app: {{.Chart.Name}}-backend
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8091

Ingress配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

测试情况

  • Service直接转发正常:
    执行命令:

    kubectl port-forward service/kuberdemo-service -n kuberdemo 8080:8090
    curl --location 'http://localhost:8080/users'
    

    返回[](数据库无数据,符合预期)。

  • 通过Ingress Controller转发返回404:
    执行命令:

    kubectl port-forward -n ingress-nginx ingress-nginx-controller-6cc5ccb977-zzbcb 8080:80
    curl --location 'http://localhost:8080/users'
    

    返回NGINX的404页面:

    <html>
    <head><title>404 Not Found</title></head>
    <body>
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
    

问题原因及修复方案

  • 核心问题:Ingress规则中指定了host: kuberdemo.info,但测试时直接用localhost访问,NGINX找不到匹配的Ingress规则,因此返回404。

  • 修复方式三选一即可:

    1. 添加本地hosts映射:
      在Windows主机的C:\Windows\System32\drivers\etc\hosts(或WSL2内部的/etc/hosts)中添加一行:

      127.0.0.1 kuberdemo.info
      

      之后直接用域名访问:

      curl --location 'http://kuberdemo.info:8080/users'
      
    2. curl请求指定Host头:
      无需修改hosts,直接在curl命令中指定Host头匹配Ingress规则:

      curl --location 'http://localhost:8080/users' --header 'Host: kuberdemo.info'
      
    3. 移除Ingress的host限制:
      如果不需要域名匹配,删除Ingress配置中的host: kuberdemo.info,这样所有发往Ingress的请求都会匹配该规则。修改后的Ingress配置:

      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:
        name: kuberdemo-ingress
      spec:
        rules:
          - http:
              paths:
                - path: /
                  pathType: Prefix
                  backend:
                    service:
                      name: kuberdemo-service
                      port:
                        number: 8090
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:12:32