Rewrite Paths with Kubernetes Ingress

2024-01-01 13:50 (1 years ago) ytyng

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.

Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011