Rewrite Paths with Kubernetes Ingress

2024-01-01 04:50 (2 years ago)
Rewrite Rhythm
Play a song themed on this article

The matched part of the regular expression in rules.http.paths.path is placed into metadata.annotations.nginx.ingress.kubernetes.io/rewrite-target.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: my-namespace
  annotations:
    ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /api/apppath/$1
spec:
  tls:
    - secretName: my-http-cert
  rules:
    - host: log.ytyng.com
      http:
        paths:
          - path: /(.*)
            pathType: Prefix
            backend:
              service:
                name: my-service-service
                port:
                  number: 8000

In this configuration, the Ingress resource defines a rule for HTTP requests. The rule specifies that any requests to the host log.ytyng.com with a path matching the regular expression /(.*) will be redirected. The matched part of the path (captured by the (.*) group) will be inserted into the rewrite target specified by the annotation nginx.ingress.kubernetes.io/rewrite-target. Consequently, requests will be rewritten to the path /api/apppath/$1, where $1 represents the captured group. The backend service my-service-service on port 8000 will handle the rewritten requests. Additionally, SSL redirection is enabled as indicated by the annotation ingress.kubernetes.io/ssl-redirect: "true", and the TLS configuration uses the secret my-http-cert for securing the connection.

Categories

Archive