Nginx Ingress - use https for backend based on path

Viewed 2326

I'm configuring ingress to pass requests to backends based on path. Only service-external needs to use HTTPS. If I add annotation nginx.ingress.kubernetes.io/backend-protocol: 'HTTPS', ingress will use https for both backends, which is not desired. Is there anyway to use https for selected backends only?

ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'
  tls:
  - secretName: my-tls
    hosts:
    - myhost.com
  rules:
  - host: myhost.com
    paths:
    - path: /external/
      serviceName: service-external
      servicePort: 443
    - path: /
      serviceName: service-api
1 Answers

As mentioned in comments, the use of an annotation in an ingress spec will enable this functionality for all paths.

The only way to achieve what you want is create a new ingress spec with the desired path. So you will have 2 ingress for the same host, see this example:

I'm configuring ingress to pass requests to backends based on path. Only service-external needs to use HTTPS. If I add annotation nginx.ingress.kubernetes.io/backend-protocol: 'HTTPS', ingress will use https for both backends, which is not desired. Is there anyway to use https for selected backends only?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-https-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/backend-protocol:  "HTTPS"
spec:
  tls:
    - hosts:
      - myapp.com
      secretName: myapp.com
  rules:
  - host: myapp.com
    http:
      paths:
      - path: "/"
        backend:
          serviceName: my-https-service
          servicePort: 443
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-http-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  tls:
    - hosts:
      - myapp.com
      secretName: myapp.com
  rules:
  - host: myapp.com
    http:
      paths:
      - path: "/notsecure"
        backend:
          serviceName: my-http-service
          servicePort: 80

The first ingress will redirect every request for https://myapp.com/ to my-https-service and if the request was to https://myapp.com/notsecure the request will be redirect to the service my-http-service over HTTP, but for the end user the connection still HTTPS.

Related