简体   繁体   中英

Nginx Ingress URL rewriting

I have created an EKS cluster and deployed nginx ingress controller to expose my simple node.js app to the world. for doing so nginx ingress controller has service type as loadbalancer and an ELB is provisioned which is publicly accessible.

now i want to send hit coming from user on http://beat.test.com/test/ to "/" on backend nodejs service.

but i am not able to do so and continuously getting 404.

nodesjs app deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nodejs-app
  name: nodejs-app-deployment
  namespace: default
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nodejs-app
  template:
    metadata:
      labels:
        app: nodejs-app
    spec:
      containers:
      - name: nodejs-app
        image: image:tag
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          requests:
            memory: 2024Mi
          limits:
            memory: 4096Mi

nodejs service.yaml

---
kind: Service
apiVersion: v1
metadata:
  name:  nodejs-app-service
  namespace: default
spec:
  selector:
    app:  nodejs-app
  type:  ClusterIP
  ports:
    - port:  9080
      targetPort:  8080
      protocol: TCP

nodejs app ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nodejs-app-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:

     - host: beta.test.com
       http:
         paths:
           - backend:
               serviceName: nodejs-app-service
               servicePort: 9080
             path: /test(/|$)(.*)

This is a Community Wiki answer so feel free to edit it and add any additional details you consider important.

now i want to send hit coming from user on http://beat.test.com/test/ to "/" on backend nodejs service.

It can be don the way, Tushar Mahajan proposed in his answer :

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/service-upstream: "true"
  name: rule-name
  namespace: default
spec:
  rules:
  - host: hostname
    http:
      paths:
      - backend:
          serviceName: frontend-service
          servicePort: port-number
        path: /(.*)
      - backend:
          serviceName: backend-service
          servicePort: port-number
        path: /api/(.*)

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