strip-prefix

The gateway publishes /demo/orders, the service only knows /orders. The most used filter of the catalogue: it is what lets one application be mounted under a path of your choosing without the application knowing.

Parameters

Name Type Required What it does
parts integer no Number of leading segments removed. Default: 1. Must be at least 1.
announcePrefix boolean no Tell the service where it is published, as X-Forwarded-Prefix. Default: true.

Example

predicates:
  - type: path
    args:
      patterns:
        - /demo/**
filters:
  - type: strip-prefix
    args:
      parts: 1

/demo/orders/8814 reaches the service as /orders/8814, with X-Forwarded-Prefix: /demo.

Notes

X-Forwarded-Prefix is how a service that builds its own links learns where it lives. Without it, an application seeing /orders writes /orders, the browser follows it, and it lands outside the route. Spring reads the header through ForwardedHeaderFilter, nginx sets it; a service that only ever answers does not need it.

Whatever a caller sent under that name is purged, on every route, even one with no strip-prefix: a caller sending their own prefix would make the service write its links wherever they asked.

On the way back, the prefix is restored on the two things a browser follows without asking: a page's <base href> (<base href="/"> becomes <base href="/demo/">, or its scripts are looked for at the gateway's root) and the Location of a redirect the service writes from its own root or to its own address. A base or a redirect already under the prefix, relative, or to another site is left alone.

Two strip-prefix in a row work: the announced prefix widens to what was actually consumed rather than being overwritten.

An application that builds absolute links usually wants preserve-host as well: the prefix says where, the Host says under which name.