Rewrite rule or server snippet in Nginx ingress with query parameter

Viewed 562

I have a cluster with Nginx ingress. I receive an API request, for example:

/api/v1/user?json={query}

I want to redirect this request with ingress to my service. I want to modify it like this:

/api/v2/user/{query}
1 Answers

Assuming your domain name is example.com, and you have a service called example-service exposing port 80, you can achieve this task by defining the following ingress rule.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
       if ($arg_json) {
           return 302 https://example.com/api/v2/user/$arg_json;
       }
    nginx.ingress.kubernetes.io/use-regex: 'true'
  name: ingress-rule
  namespace: default
spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          service:
            name: example-service
            port:
              number: 80
        path: /api/v1/user(.*)
        pathType: Prefix

Related