K8S Nginx Ingress issue - invalid condition

6/2/2020

I have trouble about nginx ingress using condition

Install Nginx Ingress via helm instal (tested in both nginx-ingress-1.16 and nginx-ingress-1.36

I am try to follow https://stackoverflow.com/questions/59230411/ingress-nginx-redirect-from-www-to-https Setup some condition

like

    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ( $host = "mydomain.co" ) {
        rewrite ^ https://www.mydomain.co$uri permanent;
      }

When apply the ingress rule, nginx ingress start reload in fail status

-------------------------------------------------------------------------------
W0602 07:35:36.244415       6 queue.go:130] requeuing vincent/demoheader-ingress, err
-------------------------------------------------------------------------------
Error: exit status 1
2020/06/02 07:35:36 [notice] 982#982: ModSecurity-nginx v1.0.0
2020/06/02 07:35:36 [emerg] 982#982: invalid condition "~" in /tmp/nginx-cfg971999838:530
nginx: [emerg] invalid condition "~" in /tmp/nginx-cfg971999838:530
nginx: configuration file /tmp/nginx-cfg971999838 test failed

My Full ingress rule

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: demoheader-ingress
  namespace: namespace
  annotations:
    kubernetes.io/ingress.class: nginx-temp
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
    nginx.ingress.kubernetes.io/configuration-snippet: |
        if ( $uri ~* ^/xx/(.*) ) {
          rewrite ^ https://www.xxx.co permanent;
        }
spec:
  rules:
  - host: mydomain
    http:
      paths:
      - backend:
          serviceName: header-headerv1
          servicePort: 80
        path: /
EOF

Any idea ?

-- Vincent Ngai
kubernetes
nginx
nginx-ingress

1 Answer

6/2/2020

OK i know what happen in here

I encount a wired issue for k8s apply stuff …. While official document told you , you can apply object by this method

cat <<EOF | kubectl apply -f - 
xxx
yyy
eee
EOF

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

However for ingress rule , if you under a specific condition such like this

    nginx.ingress.kubernetes.io/configuration-snippet: |
        if ( $host = ^mydomain ) {
          rewrite ^ https://www.mydomain$uri permanent;
        }

you are unable to make the nginx working again (never config reload success, it will affect the following config change)

Until you delete the ingress rule and re-apply by kubectl apply -f the-ingress-file

-- Vincent Ngai
Source: StackOverflow