Path Rewriting

Rewrite request paths before forwarding to upstream

Category: Practical
Task: Rewrite request paths before forwarding to upstream

Prerequisites: The product runtime and any backend services referenced by this configuration.

Run it: Use ghcr.io/praxis-proxy/praxis:0.7.2 and follow the first reverse-proxy tutorial to mount and start the configuration.

This configuration comes from the selected release. The example has not been run here; external services are not bundled.

Download the source file.

# Path Rewriting
#
# Rewrite request paths before forwarding to upstream.
#
# Rewrite pipeline (per request):
#
#   Incoming path
#        |
#        v
#   +---------------------+
#   | strip_prefix        |  /api/v1/users -> /users
#   +---------------------+
#        |
#        v
#   +---------------------+
#   | add_prefix          |  (if set) /users -> /svc/users
#   +---------------------+
#        |
#        v
#   +---------------------+
#   | replace (regex)     |  /old/resource -> /new/resource
#   +---------------------+
#        |
#        v
#   Rewritten path (used by router)
#
listeners:
  - name: default
    address: "127.0.0.1:8080"
    filter_chains:
      - main

filter_chains:
  - name: main
    filters:
      - filter: router
        routes:
          - path_prefix: "/"
            cluster: backend

      - filter: path_rewrite
        strip_prefix: "/api/v1"
        conditions:
          - when:
              path_prefix: "/api/v1"

      - filter: path_rewrite
        replace:
          pattern: "^/old/(.*)"
          replacement: "/new/$1"
        # Permits a later rewrite filter to override an earlier one.
        # Security consideration: filter order matters; the last
        # rewrite wins, so misordering can bypass path-based ACLs.
        allow_rewrite_override: true
        conditions:
          - when:
              path_prefix: "/old/"

      - filter: path_rewrite
        add_prefix: "/svc"
        allow_rewrite_override: true
        conditions:
          - when:
              path_prefix: "/items"

      - filter: load_balancer
        clusters:
          - name: backend
            endpoints:
              - "127.0.0.1:3000"

insecure_options:
  allow_private_endpoints: true # example proxies to local backends