Nginx Ingress rewrite target with additional subpaths

Asking for help? Comment out what you need so we can get more information to help you!

Cluster information:

Kubernetes version: 1.20
Installation method: Helm chart
Nginx image: k8s.gcr.io/ingress-nginx/controller:v0.45.0

I have an ingress that requires a sub-path to be explicitly declared, or the websocket it is used for won’t work internally. I have it set up like this when using the “root” path “/” for the general server and “/socket.io/” giving access to the websocket.

MWE

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-server
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-server
            port:
              number: 5000
      - path: /socket.io/.*
        pathType: Prefix
        backend:
          service:
            name: my-server
            port:
              number: 5000

I would now like to deploy this on a sub-path for testing purposes but when I add rewrite-target things break for the websocket.

Non-working example

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-server
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: testing(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: my-server
            port:
              number: 5000
      - path: /testing/socket.io(/|$)(.*) # <--- This is what breaks
        pathType: Prefix
        backend:
          service:
            name: my-server
            port:
              number: 5000

The general path forwarding from “http://my-ip/testing” → “/” in the deployment is working fine but I can’t seem to get the websocket setup so that “my-ip/testing/socket.io/.*” → “/socket.io/.*” on the deployment properly.
How do I set this up with the (/|$) and (.*) in the path to achieve this?
Is there a different way I can do this?

1 Like

I have solved this in a different way, would still be interested to know if there is an ingress only solution.

1 Like

Hey Kerry, I am dealing with the same issue right now.
Would you share your solution with us/me? It would be very appreciated.
Thank you a lot!

Hi all,

I have also issue with websocket. If you have any solution or blog then please share with me