3

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
  • 6,861
  • 13
  • 54
  • 81
  • did you try rewrite feature https://www.nginx.com/blog/creating-nginx-rewrite-rules/ – Prateek Jain Sep 30 '21 at 10:13
  • I am using: `nginx.ingress.kubernetes.io/rewrite-target: /$2` already. – jbssm Sep 30 '21 at 10:36
  • Which image for the container are you using? – Mikolaj S. Oct 01 '21 at 10:23
  • @MikolajS. I meant this as a general question. I know some containers have some way (different ways) of setting the rootpath, but I am searching for a general solution. – jbssm Oct 01 '21 at 11:15
  • 1
    @WytrzymałyWiktor No, not really. I do reckon it is a problem with Kubernetes Nginx Ingress itself (either if other ingress like Traefik have a solution for the issue or not, I still don't know). But truth is, although there are good answers here, nobody actually gave me a fix for the problem. – jbssm Oct 05 '21 at 13:21

2 Answers2

2

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
  • 27,020
  • 6
  • 48
  • 102
1

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.
  • 2,850
  • 1
  • 5
  • 17