nginx-ingress returning 404 with multiple ingresses files

5/13/2019

I'm trying to set up a kubernetes cluster in GKE using nginx-ingress to handle request routing. I have two different projects which I would like to host within the same domain, with each managing their own ingress definition. The readme here seems to show something similar, so I assume this should be possible.

When I deploy either ingress individually, everything works great and I can access the routes I would expect. However, when I add both at the same time, only the ingress with the metadata.name value that comes first alphabetically will reach the intended backend, while the other ingress will return a 404 from nginx-ingress.

If I switch the metadata.name values, this behavior is consistent (the ingress that has the first alphabetical name will work), so I don't think it has to do with the routes themselves or the services / pods involved but rather something to do with how nginx-ingress is handling the ingress names.

I've tried various versions of the nginx-ingress-controller:

  • quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.24.1
  • quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.24.0
  • quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.23.0
  • quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.22.0

I've also tried forcing regex route matching (using nginx.ingress.kubernetes.io/rewrite-target: /), swapping the names on the ingress, deploying the projects to different namespaces, and changing the paths to be entirely distinct with no luck - only one ingress file will be used at a time.

Finally, I tried making one ingress file with both definitions in it (so there's only one ingress name in play), and that works fine. Comparing the nginx configuration of the unified, working setup with a non-working one, the only line that is different is the "set $ingress_name", e.g.:

set $ingress_name   "test-ingress-1";

vs

set $ingress_name   "test-unified-ingress";

Here are the ingresses (with the hostname changed):

test-ingress-1.yaml:

apiVersion: extensions/v1beta1                               
kind: Ingress                                                
metadata:                                                    
  annotations:                                               
    kubernetes.io/ingress.class: nginx                 
    nginx.ingress.kubernetes.io/from-to-www-redirect: "false"                          
  name: test-ingress-1                                       
  namespace: default                                         
spec:                                                        
  rules:                                                     
  - host: test.com                               
    http:                                                    
      paths:                                                 
      - backend:                                             
          serviceName: test-frontend                         
          servicePort: 80                                    
        path: /test                    
status:                                                      
  loadBalancer: {}                                           

test-ingress-2.yaml:

apiVersion: extensions/v1beta1                               
kind: Ingress                                                
metadata:                                                    
  annotations:                                               
    kubernetes.io/ingress.class: nginx                 
    nginx.ingress.kubernetes.io/from-to-www-redirect: "false"                          
  name: test-ingress-2                                       
  namespace: default                                         
spec:                                                        
  rules:                                                     
  - host: test.com                               
    http:                                                    
      paths:                                                 
      - backend:                                             
          serviceName: test-backend                         
          servicePort: 80                                    
        path: /api/test                    
status:                                                      
  loadBalancer: {} 

I would expect those two separate ingress files to configure nginx together, but haven't had success. Is there something that I'm missing or doing wrong?

Thank you for any assistance!

-- Patrick
kubernetes
nginx-ingress

2 Answers

5/14/2019

Why you just don't put multiple paths into one ingress file?

apiVersion: extensions/v1beta1                               
kind: Ingress                                                
metadata:                                                    
  annotations:                                               
    kubernetes.io/ingress.class: nginx                 
    nginx.ingress.kubernetes.io/from-to-www-redirect: "false"                          
  name: test-ingress-1                                       
  namespace: default                                         
spec:                                                        
  rules:                                                     
  - host: test.com                               
    http:                                                    
      paths:
      - path: /test                                                 
        backend:                                             
          serviceName: test-frontend                         
          servicePort: 80                                    
      - path: /api/test
        backend:                                             
          serviceName: test-backend                         
          servicePort: 80                                    
status:                                                      
  loadBalancer: {}  

Or use rewrite-target as suggested below

-- A_Suh
Source: StackOverflow

5/14/2019

Can you try adding this annotation and test it if it's work or not.

nginx.ingress.kubernetes.io/rewrite-target: /
-- Harsh Manvar
Source: StackOverflow