Reference nginx-ingress host name to internal service

2/8/2020

After creating all require resource and config the nginx-ingress controller.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: user-api
  strategy: {}
  template:
    metadata:
      labels:
        app: user-api
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      containers:
      - name: user-api
        image: doumeyi/user-api-amd64:1.0
        ports:
        - name: user-api
          containerPort: 3000
        resources: {}
---
apiVersion: v1
kind: Service
metadata:
  name: user-api
spec:
  selector:
    app: user-api
  ports:
  - name: user-api
    port: 3000
    targetPort: 3000
  type: LoadBalancer
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: example.com
      http:
        paths:
        - path: /user-api
          backend:
            serviceName: user-api
            servicePort: 3000

I can view example.com show the 404 not found page, but also can not see example.com/user-api to show any message I build in user-api service.

It seems the nginx-ingress cannot resolve the host name to the internal service, how should I fix it?

-- ccd
kubernetes-ingress
nginx-ingress

1 Answer

2/8/2020

If NGINX cannot find a route to your Pod, it should not respond with 404. Instead it should give an 502 (Bad Gateway) AFAIK. I assume the 404 is from your application.

NGINX-Ingress changed behavior in 0.22 as mentioned here The ingress resource should look like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: example.com
      http:
        paths:
        - path: /user-api(/|$)(.*)
          backend:
            serviceName: user-api
-- Alex
Source: StackOverflow