---
slug: "kubernetes-ingress-でパスのリライトをする"
title: "Rewrite Paths with Kubernetes Ingress"
description: "The element matched by the regular expression in `rules.http.paths.path` within an Ingress configuration is placed into `metadata.annotations.nginx.ingress.kubernetes.io/rewrite-target`."
url: "https://www.ytyng.com/en/blog/kubernetes-ingress-でパスのリライトをする"
publish_date: "2024-01-01T04:50:47Z"
created: "2024-01-01T04:50:47Z"
updated: "2026-02-27T07:11:11.908Z"
categories: []
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250611/8c86f80d26de458a98532a61d6f7190f.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/301/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/301/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/301/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/301/featured-music-301-1.mp3", "https://media.ytyng.net/ytyng-blog/301/featured-music-301-3.mp3"]
lang: "en"
---

# Rewrite Paths with Kubernetes Ingress

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

```yaml
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.
