Passing session variables while using rewrtite-target annotation in Ingress

Viewed 104

In my ingress I have used target-rewrite annotation which is domain B service which loads in background when domain A front end loads.

How can I pass session variables/header which I give while login to domain A which is front end.

Currently, when I open front end (domain A) I enter my credentials, again it asks for credentials when it rewrite-targets to domain B in background. Any help/inputs will be highly appreciated.

1 Answers

rewrite-target it is only used to change the URI address:

nginx.ingress.kubernetes.io/rewrite-target - Target URI where the traffic must be redirected string.

Here is example yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80

In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

For example, the ingress definition above will result in the following rewrites:

  • rewrite.bar.com/something rewrites to rewrite.bar.com/
  • rewrite.bar.com/something/ rewrites to rewrite.bar.com/
  • rewrite.bar.com/something/new rewrites to rewrite.bar.com/new

Taking this into account, you are only able to operate on variables that will be included in the URI. Then you will have to set the appropriate capture group and then refer to it (similarly to the example presented above).

Another solution for storing some information (variables) is to use the sticky sessions. You can find all the necessary information and examples here.

Related