简体   繁体   中英

Nginx Ingres Redirect only on path to another server

I have setup nginx ingress like this on kubernetes

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: animefanz-ktor-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  tls:
    - hosts:
        - mydomain.com
      secretName: my-tls
  rules:
    - host: mydomain.com
      http:
        paths:
          - path: /myapp(/|$)(.*)
            backend:
              serviceName: myservice
              servicePort: 8080

So everything working fine but i want to do one thing when ever https://mydomian.com/myapp/api/history called then i want to redirect it to https://mydomain2.com/myapp2/api/history along with get params that is.

So i want to just forward one api request to another server.

I think you can configure it with nginx server-snippet / configuration-snippet annotation.


There is related stackoverflow question about that.

And examples provided by @Thanh Nguyen Van

metadata:
      annotations:
        nginx.ingress.kubernetes.io/configuration-snippet: |
         rewrite /preview https://test.app.example.com$uri permanent;

spec:
      rules:
      - host: test.example.io
        http:
          paths:
          - path: /
            backend:
              serviceName: service-1
              servicePort: 80
      - host: test.app.example.io
        http:
          paths:
          - path: /preview/*
            backend:
              serviceName: service-2
              servicePort: 80

And @Harsh Manvar

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/server-snippet: |
          location ~ /preview {
             rewrite /preview https://test.app.example.com$uri permanent;
          }
  name: staging-ingress
spec:
  rules:
  - host: test.example.io
    http:
      paths:
      - path: /
        backend:
          serviceName: service-1
          servicePort: 80
      - path: /preview/*
        backend:
          url: 
          serviceName: service-2
          servicePort: 80
  tls:
  - hosts:
    - test.example.io
    secretName: staging  

Additionally there is related github issue about that.

Hope you find this useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM