OiO.lk Blog java Kubernetes NGINX Ingress Path Rewrite Issue (noVNC Context Path Problem)
java

Kubernetes NGINX Ingress Path Rewrite Issue (noVNC Context Path Problem)


I’m deploying an application using noVNC on a Kubernetes cluster. I am using NGINX Ingress to expose the application externally and handling user-specific pods via context paths.

The Issue
Each pod is accessed through a user-specific path appended to the domain, like http://example.com/podPath. The index.html page loads correctly when accessed.
However, when clicking on internal links such as vnc.html, the path reverts to http://example.com/vnc.html, losing the context path /podPath. I need the links to maintain the context path, so they should be accessed as http://example.com/podPath/vnc.html.

What I’ve Tried

NGINX Ingress Annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/x-forwarded-prefix: /podPath
nginx.ingress.kubernetes.io/add-base-url: true

Despite these settings, the internal links still lose the context path and fall back to the root (/) path.

NGINX Ingress Path Rewrite:

I tried rewriting the paths in the Ingress using rewrite-target, but the internal links do not retain the /podPath and revert to the base domain instead.
Question
Is there a recommended way to maintain the context path when using noVNC behind NGINX Ingress?
How can I ensure that internal links always retain the podPath prefix without having to modify the noVNC HTML files? Is there any Ingress annotation or another method to solve this?

**
full code
**


public void createOrUpdateIngress(PodRequest podRequest) throws ApiException {
        String ingressName = podRequest.getName() + "-ingress";
        String serviceName = podRequest.getName() + "-service";
        String baseDomain = "example.com";  
        String podPath = "/" + podRequest.getName();  
        System.out.println("podPath = " + podPath);

        V1Ingress ingress = new V1Ingress()
                .metadata(new V1ObjectMeta()
                        .name(ingressName)
                        .annotations(Map.of(
                                "nginx.ingress.kubernetes.io/rewrite-target", podPath + "/$1",  
                                "nginx.ingress.kubernetes.io/x-forwarded-prefix", podPath, 
                                "nginx.ingress.kubernetes.io/add-base-url", "true" 
                        )))
                .spec(new V1IngressSpec()
                        .ingressClassName("nginx")
                        .rules(Arrays.asList(
                                new V1IngressRule()
                                        .host(baseDomain)
                                        .http(new V1HTTPIngressRuleValue()
                                                .paths(Arrays.asList(
                                                        new V1HTTPIngressPath()
                                                                .path(podPath + "(.*)")  
                                                                .pathType("Prefix")
                                                                .backend(new V1IngressBackend()
                                                                        .service(new V1IngressServiceBackend()
                                                                                .name(serviceName)
                                                                                .port(new V1ServiceBackendPort().number(6901))))))))));

        NetworkingV1Api networkingV1Api = new NetworkingV1Api();
        try {
            networkingV1Api.readNamespacedIngress(ingressName, defaultNamespace, null);
            networkingV1Api.replaceNamespacedIngress(ingressName, defaultNamespace, ingress, null, null, null, null);
        } catch (ApiException e) {
            if (e.getCode() == 404) {
                networkingV1Api.createNamespacedIngress(defaultNamespace, ingress, null, null, null, null);
            } else {
                throw e;
            }
        }
    }

Thanks in advance!



You need to sign in to view this answers

Exit mobile version