Hosting webapp with relative URLs behind Kubernetes NGINX ingress controller

3/28/2019

I am hosting a web application inside a Kubernetes 1.13 cluster behind an NGINX ingress controller. The Ingress specifies path: /my-webapp/?(.*) and annotations: { nginx.ingress.kubernetes.io/rewrite-target: /$1 } such that the webapp can be reached from outside the cluster at http://my-cluster/my-webapp. (The ingress controller is exposed as my-cluster.)

One remaining problem is that the webapp contains "relative" URLs that refer e.g. to CGI scripts and to CSS stylesheets. For instance, the stylesheet in <link rel="stylesheet" type="text/css" href="/my-stylesheet.css" /> currently does not load. I assume this is because the browser requests it at the wrong URL (http://my-cluster/my-webapp/my-stylesheet.css would be right) and that some more annotations are needed.

What is the correct configuration is such a case?

UPDATE The inspector reveals that the browser currently requests the stylesheet from URL http://my-cluster/my-stylesheet.css, which is indeed wrong and needs to be fixed.

UPDATE This looks like a related problem with an NGINX reverse proxy in general, not a Kubernetes NGINX ingress controller in particular. I wonder if and how the suggested recipes can also serve in this particular case. For a start, I have tried switching to a relative URL for referencing the stylesheet (per recipe One in the accepted answer), but this also has not worked so far: <link rel="stylesheet" type="text/css" href="my-stylesheet.css" />, the browser apparently still tries to get the stylesheet from http://my-cluster/my-stylesheet.css, although it displays http://my-cluster/my-webapp in the URL bar and the inspector reports the same URL as baseURI.

-- rookie099
kubernetes-ingress
nginx-reverse-proxy

2 Answers

5/5/2020

In my situation, I must replace URL-s from wsdl service page and this my solution code that was written thanks to the above code

nginx.ingress.kubernetes.io/configuration-snippet: |
  sub_filter 'http://example.com:80/project/domain/' 'http://example.com:80${PUBLISH_PATH}/project/domain/';
  sub_filter_once off;
  sub_filter_types text/xml;

${PUBLISH_PATH} - is the kuber domain specific

-- Жасулан Бердибеков
Source: StackOverflow

3/29/2019

Now this combination of annotations seems to do the trick:

annotations:
  nginx.ingress.kubernetes.io/configuration-snippet: |
    proxy_set_header Accept-Encoding "";
    sub_filter '<head>' '<head> <base href="/my-webapp/">';
  nginx.ingress.kubernetes.io/rewrite-target: /$1
  nginx.ingress.kubernetes.io/use-regex: "true"

I am currently using this version of the ingress controller:

-------------------------------------------------------------------------------
NGINX Ingress controller
  Release:    0.23.0
  Build:      git-be1329b22
  Repository: https://github.com/kubernetes/ingress-nginx
-------------------------------------------------------------------------------
-- rookie099
Source: StackOverflow