Using Nginx path prefix as a Kubernetes Ingress, breaks everything under subpaths even when using rewrite-target

9/30/2021

This example is specifically about Nextcloud, although I had the same (unsolved issue) in the past with others.

What I simply want to do, is to access nextcloud under www.myserver.com/nextcloud.

I am able to kind of accessing Nextcloud front page with my present setup, but everything that's not directly under the basepath, is broken. Images and JS for instance.

Sure enough, I can manually enter in my browser web address something like www.myserver.com/nextcloud/core/css/guest.css, and it's there. But the issue is that the front page from Nextcloud, tries to access everything under: www.myserver.com/core/css/guest.css

Here is my ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nextcloud
  namespace: homeserver
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-staging
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $http_connection;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: www.myserver.com
    http:
      paths:
      - path: /nextcloud(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: nextcloud
            port:
              number: 80
-- jbssm
kubernetes
kubernetes-ingress
nginx
nginx-ingress

2 Answers

10/1/2021

You should have to use the relative path in backend or HTML if you are using that.

However you can do one thing

if your all request getting by the application at : www.myserver.com/core and there is no other folder exist or endpoint

you can create some redirection rules like :

www.myserver.com/core -> www.myserver.com/nextcloud/

once redirect redirected to new URL further another ingress that you have created will take care of the path and serve.

Ingrss example

metadata:
  name: ingress-test
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
    nginx.ingress.kubernetes.io/location-snippet: |
      location = /core/ {
        proxy_pass http://[hostname]/nextcloud/;
        }
spec:

still, it depends on your application config and structure.

-- Harsh Manvar
Source: StackOverflow

10/1/2021

Well, there is no general solution as the problem is not on the Ingress configuration side itself; the problem is, as you noticed, that your application is generating link for dependencies using absolute path /, (like /core/css etc.) which of course is not working as Ingress does not have definition for /core/css etc. The dependencies are available under /nextcloud/core/css path. Your app is not aware that it is running under /nextcloud. Ingress can't change calls that are made from inside the application, you must configure properly your application.

How to resolve it in proper way? Fix your application. Configure it to use proper basepath (by passing proper environment variable) or to use relative paths. Some examples:

-- Mikolaj S.
Source: StackOverflow