0

I need to rewrite the target only for the root path, but all others paths must work normally.

I tried the following rules, but it doesn't redirect to /somePath. Instead, it goes to the normal root.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-test-web-app-other
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - test.example.com
      secretName: tls-example
  rules:
    - host: test.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: webapp
                port:
                  name: http
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-test-web-app-root
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /somePath
    nginx.ingress.kubernetes.io/upstream-vhost: "example.com"
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - test.example.com
      secretName: tls-example
  rules:
    - host: test.example.com
      http:
        paths:
          - path: /
            pathType: Exact
            backend:
              service:
                name: webapp
                port:
                  name: http
---

If I try nginx.ingress.kubernetes.io/use-regex: "true" with path: "/(.+)" for ingress-test-web-app-other, I get 404 on opening root path.

Is there something I'm missing?

Roman
  • 51
  • 6

1 Answers1

0

If your application was listening on /somePath, in other words if it had its root path at /somePath rather than at /, all requests sent to / would be redirected to /somePath. Check the example here.

As you can see here:

nginx.ingress.kubernetes.io/app-root annotation defines the Application Root that the Controller must redirect if it's in '/' context.

And use the above annotation and below examples, have a try and post the errors if you get any.

As per ingress doc , Create an Ingress rule with an app-root annotation as below :

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/app-root: /app1
  name: approot
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: approot.bar.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80
Hemanth Kumar
  • 2,728
  • 1
  • 4
  • 19
  • Sorry, maybe I was not clear. I have a domain like www.example.com. There is a page www.example.com/test on it. And I need to add a subdomain like: test.example.com that will be linked to this path, but all other relative paths to assets have to be linked to the main service. So for instance, a path like test.example.com/image/pic.jpg should not follow www.example.com/test/image/pic.jpg, instead, it should be linked to www.example.com/image/pic.jpg. – Roman Dec 14 '22 at 09:16