Nginx Ingress rewrite target with additional subpaths
Asked Answered
L

0

1

Cluster information:

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

I am running a flask server with flask-socketio connections on a cloud based server inside kubernetes. 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?

Linear answered 15/9, 2021 at 13:45 Comment(5)
Hello @Linear Did you try to use only one rewrite rule for path: testing(/|$)(.*) without additional for socket.io?Francklin
@AndrewSkorkin, yes. It worked for the general stuff but the socketio implementation inside the app crashes because it can't access itself(?). Something inside the app is unable to access it at least and it breaks.Linear
Hi @Kajsa, does your issue still persist?Hylo
Hey @MikołajGłodziak I have managed to solve it by removing the rewrite and making changes inside the server itself to support additional path prefixes for both the http and websocket paths. This is a "workaround" that was possible because I was able to edit the server code. I'd still be interested in an ingress only solution.Linear
Hey @WytrzymałyWiktor, see comment above.Linear

© 2022 - 2024 — McMap. All rights reserved.