Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How can I configure my ingress to redirect all HTTP traffic to HTTPS unless it matches a path?

We have a kubernetes cluster set up on GCP that by default redirects all traffic from HTTP to HTTPS. Due to limitations on some of our IoT devices we are unable to use an HTTPS library on the devices that would be calling this endpoint. To avoid having all traffic go through on HTTP, I would like to configure it such that url.io/http-api does not redirect to http, but otherwise url.io/ will redirect to HTTPS. Ideally both of these point to the same service, just that one redirects and the other doesn't. I am relatively new to kubernetes configurations so this might be easier than I think, but I've been struggling with it for the greater part of the day. Any advice would be greatly appreciated.

So far I've tried experimenting with different configurations in the ingress yaml. I have two paths set up in http, but they have the same configuration, and I'm unsure of what changes to make to handle them differently if I want them both pointing to the same service.

spec:
  rules:
  - host: url.io
    http:
      paths:
      - path: /http-api
        pathtype: Prefix
        backend: 
          service: 
            name: myapp-svc
            port:
              number: 80
      - backend:
          service:
            name: myapp-svc
            port:
              number: 80
        path: /
        pathType: Prefix
  tls:
  - hosts:
    - url.io
    secretName: myapp-tls

I currently have `nginx.ingress.kubernetes.io/ssl-redirect: "false"` set in my ingress annotations, which does allow me to make http requests successfully, but this also prevents any http traffic from being redirected. Ideally I'll have it so that only traffic going to url.io/http-api will stay on http, but everything else will reroute to https.

---

I made this post on stack overflow a few days ago but haven't gotten any responses, so I thought I'd try here too. If you want to reply to the stack overflow post, it's linked here: https://stackoverflow.com/questions/76082639/using-kubernetes-in-gcp-how-can-i-configure-my-ingress-...

0 1 2,762
1 REPLY 1

Hi @auranykh ,

Have you happen to check this discussion? It has multiple related and useful anwers that you can try with your setup. Moreover, I suggest you try this step or solution provided by a community member, as it provides a detailed guide that you can follow. 

Top Labels in this Space
Top Solution Authors