Unable to create route rule with kubernete nginx ingress controller

1/21/2019

I am trying to use kubernetes nginx ingress controller: (quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.22.0). Below is my ingress object.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: cc-store-ingress
 annotations:
  kubernetes.io/ingress.class: "nginx"
  nginx.ingress.kubernetes.io/rewrite-target: /$1
  nginx.ingress.kubernetes.io/add-base-url: "true"
  #nginx.ingress.kubernetes.io/configuration-snippet: |
  # sub_filter "http://my-ip:30021/" "http://my-ip:30021/app/";
  # sub_filter_once off;
spec:
 #tls:
 #- secretName: tls-secret
 rules:
 - host: my-ip
  http:
   paths:
   - path: /app/?(.*)
    backend:
     serviceName: appsvc
     servicePort: 7201

When I try to access the this service via ingress I hit a blank page, which I understand is because response (set of few java scripts , css and others) are returning to my-ip:30021/ instead of my-ip:30021/app. (checked the nginx logs initial connection gives 200 response subsequent loading of css and js are failing with 404)

Is there a way to overcome this? Neither "sub_filter" nor add-base-url annotations helped.

Is there any way to achieve the path rewriting for response. Would using any other ingress controller (instead of nginx) can make this easier to overcome ?

-- user3540835
kubernetes
kubernetes-ingress
nginx

1 Answer

10/31/2019

This is a sample of how I did adding base path to a service which completely doesn't support it. As well a solution for handling redirects to the urls without base path.

annotations:
  kubernetes.io/ingress.class: nginx
  # catch $1 from 'path' capture group
  nginx.ingress.kubernetes.io/rewrite-target: /$1
  # handle redirects
  # nginx.ingress.kubernetes.io/proxy-redirect-from: http://<host>/
  # nginx.ingress.kubernetes.io/proxy-redirect-to: /<basePath>/
  nginx.ingress.kubernetes.io/configuration-snippet:
    proxy_set_header Accept-Encoding "";
    sub_filter_last_modified off;

    # add base path to all static resources
    sub_filter '<head>' '<head> <base href="/<basePath>/">';
    sub_filter 'href="/' 'href="';
    sub_filter 'src="/' 'src="';

    # set types of files to 'sub-filter'
    sub_filter_once off;
    sub_filter_types text/html text/css text/javascript application/javascript;
...
    - path: /<basePath>/?(.*)
-- Aliaksandr Sasnouskikh
Source: StackOverflow