Rewrite with nginx-ingress controller

7/14/2020

I am running celery flower on port inside Kubernetes with nginx-ingress controller

I want to do a rewrite where requests to /flower/(.*) request goes to /$1 according to their documentation: https://flower.readthedocs.io/en/latest/config.html?highlight=nginx#url-prefix

server {
    listen 80;
    server_name example.com;

    location /flower/ {
        rewrite ^/flower/(.*)$ /$1 break;
        proxy_pass http://example.com:5555;
        proxy_set_header Host $host;
    }

}

I have come up with the following ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: backend-airflow-ingress
  namespace: edna
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/use-regex: "true"
    ingress.kubernetes.io/rewrite-target: /$2
    # nginx.ingress.kubernetes.io/app-root: /flower
spec:
  rules:
  - host:
    http:
      paths:
      - path: /flower(/|$)(.*)
        backend:
          serviceName: airflow-flower-service
          servicePort: 5555

Inside POD running flower, I successfully get

curl localhost:5555/dashboard

However if get into the POD running Nginx controller, then it fails.

curl localhost/flower/dashboard

I get response by the flower:

<div class="span12">
<p>

Error, page not found

</p>
</div>

this is what I see inside nginx.conf in nginx-controller pod

server {                                                                                                                                                           
                server_name _ ;                                                                                                                                            
                                                                                                                                                                           
                listen 80 default_server reuseport backlog=511 ;                                                                                                           
                listen 443 default_server reuseport backlog=511 ssl http2 ;                                                                                                
                                                                                                                                                                           
                set $proxy_upstream_name "-";                                                                                                                              
                                                                                                                                                                           
                ssl_certificate_by_lua_block {                                                                                                                             
                        certificate.call()                                                                                                                                 
                }                                                                                                                                                          
                                                                                                                                                                           
                location ~* "^/flower(/|$)(.*)" {                                                                                                                          
                                                                                                                                                                           
                        set $namespace      "edna";                                                                                                                        
                        set $ingress_name   "backend-airflow-ingress";                                                                                                     
                        set $service_name   "";                                                                                                                            
                        set $service_port   "";                                                                                                                            
                set $location_path  "/flower(/|${literal_dollar})(.*)";                                                                                                    
                                                                                                                                                                           
                        rewrite_by_lua_block {                                                                                                                             
                                lua_ingress.rewrite({                                                                                                                      
                                        force_ssl_redirect = false,                                                                                                        
                                        ssl_redirect = true,                                                                                                               
                                        force_no_ssl_redirect = false,                                                                                                     
                                        use_port_in_redirects = false,                                                                                                     
                                })                                                                                                                                         
                                balancer.rewrite()                                                                                                                         
                                plugins.run()                                                                                                                              
                        }                                                                                                                                                  
                                                                                                                                                                           
                        # be careful with `access_by_lua_block` and `satisfy any` directives as satisfy any                                                                
                        # will always succeed when there's `access_by_lua_block` that does not have any lua code doing `ngx.exit(ngx.DECLINED)`                            
                        # other authentication method such as basic auth or external auth useless - all requests will be allowed.                                          
                        #access_by_lua_block {                                                                                                                             
                        #}                                                                                                                                                 
                                                                                                                                                                           
                        header_filter_by_lua_block {                                                                                                                       
                                lua_ingress.header()                                                                                                                       
                                plugins.run()                                                                                                                              
                        }                                                                                                                                                  
                                                                                                                                                                           
                        body_filter_by_lua_block {                                                                                                                         
                        }             
-- DmitrySemenov
kubernetes
kubernetes-ingress
nginx
nginx-ingress

1 Answer

7/14/2020

Ok figured this out

ingress.kubernetes.io/rewrite-target: /$2

should be in my case a different annotation

nginx.ingress.kubernetes.io/rewrite-target: /$2
-- DmitrySemenov
Source: StackOverflow