Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

A gateway in front of 50 MVC services adds and strips path prefixes, and services generate wrong redirect and Location URLs. How would you fix this consistently?

The services build redirect and Location URLs from the request they receive, which carries the gateway's internal host, scheme and path, not the ones the client used. They must learn the original host, scheme and prefix from forwarded headers, so we configure server.forward-headers-strategy in every service, make the gateway set one agreed header convention, trust those headers only when they come from the gateway, and test link generation through the gateway in integration tests.

Why this question is asked

When a gateway strips /shop from /shop/orders and forwards to http://orders:8080/orders, everything works until a service builds a URL. Then a client receives Location: http://orders:8080/orders/42, an internal address it cannot reach, or a redirect that lost the /shop prefix. With 50 services, fixing each one by hand never ends. Interviewers ask this because the right answer is a consistent platform rule, not a code patch. In this answer, we will see why the URLs are wrong, how forwarded headers fix it, how Spring Boot applies them, why trust matters, and how to keep 50 services consistent and tested.

1Why the Generated URLs Are Wrong

When a service builds an absolute URL, for example with ServletUriComponentsBuilder for a 201 Created response or for a redirect, it takes the scheme, host, port and path from the current request. Behind a gateway, that request is the gateway's internal call: scheme http, host orders, port 8080, and the path after the prefix was stripped. The service has no way to know that the client actually called https://api.example.com/shop/orders.

Java
@PostMapping("/orders")
ResponseEntity<OrderDto> create(@RequestBody NewOrder body) {
OrderDto o = service.create(body);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(o.id()).toUri();
return ResponseEntity.created(location).body(o);
}
// client calls: POST https://api.example.com/shop/orders
// gateway calls: POST http://orders:8080/orders
// response: Location: http://orders:8080/orders/42 (wrong scheme, host and prefix)
Step throughHow the original URL is lost
ClientGatewayOrders service
POST https://api.example.com/shop/orders
POST http://orders:8080/orders (prefix stripped)
build Location from the request it sees
201 Location: http://orders:8080/orders/42
internal URL leaks to the client

5 messages in order. Play them, or step through them one at a time.

2What Forwarded Headers Carry

The fix is for the gateway to tell the service what the original request looked like. There are two conventions. The de facto X-Forwarded headers: X-Forwarded-Proto for the scheme, X-Forwarded-Host for the host, X-Forwarded-Port, X-Forwarded-For for the client address, and X-Forwarded-Prefix for a stripped path prefix. And the standard Forwarded header from RFC 7239, which carries proto, host and for in one header but has no standard field for a prefix.

For our gateway, the headers would be X-Forwarded-Proto: https, X-Forwarded-Host: api.example.com and X-Forwarded-Prefix: /shop. A service that honours them can rebuild the original URL.

3Configuring Spring Boot to Use Them

Spring Boot has one property for this, server.forward-headers-strategy. With native, the embedded server applies the headers itself, for Tomcat through its RemoteIpValve, which handles the scheme, host, port and client address. With framework, Spring registers ForwardedHeaderFilter, which wraps the request so that getScheme, getServerName, getRequestURI and the context path reflect the forwarded values, including X-Forwarded-Prefix.

Java
# every service, set by the shared platform starter
server.forward-headers-strategy=framework
// same controller, now with forwarded headers from the gateway:
// X-Forwarded-Proto: https, X-Forwarded-Host: api.example.com, X-Forwarded-Prefix: /shop
// response: Location: https://api.example.com/shop/orders/42
// redirect:/orders/42 from an MVC controller -> Location: https://api.example.com/shop/orders/42

Notice that the controller code did not change. Because the filter changes what the request reports, every component that builds URLs, ServletUriComponentsBuilder, redirect: view names, Spring HATEOAS links and Spring Security's login redirects, picks up the correct values. Since prefix handling is the reason here, framework is the usual choice; check your server version if you prefer native, since prefix support differs.

4Trusting Headers Only From the Gateway

Forwarded headers are just headers, and a client can send them too. If a service trusts whatever arrives, an attacker can send X-Forwarded-Host: evil.example and make the service generate password reset links or redirects pointing to the attacker's site, or fake X-Forwarded-For to bypass IP-based rules.

So the rule has two halves. The gateway must remove any forwarded headers from incoming client requests and set its own, never append to what the client sent. And services must only be reachable through the gateway, enforced with network policies, so that no request with forged headers can arrive directly. ForwardedHeaderFilter trusts every request it sees, which is why the network rule matters; with the native strategy, Tomcat also checks that the sender is a trusted proxy through server.tomcat.remoteip.internal-proxies.

Step throughHandling forwarded headers safely
  1. Client request reaches the gatewaymay contain forged X-Forwarded-*
  2. Gateway strips client forwarded headersnever appends to them
  3. Gateway sets Proto, Host, Prefixone agreed convention
  4. Request from inside the trusted network?
    YesService applies the headersNoRejected by network policy
  5. Service builds URLshttps://api.example.com/shop/...

5 steps in order. Play them, or step through them one at a time.

5One Convention for 50 Services

Consistency is the real fix. The platform team writes down one convention: the gateway sends X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port and X-Forwarded-Prefix, and does not send Forwarded, because mixing the two leads to services reading different values. Every service gets server.forward-headers-strategy=framework from a shared starter rather than from each team's memory. The gateway configuration, for example Spring Cloud Gateway with its forwarded header filters and StripPrefix routes, is the single place where prefixes are defined.

Services also stop hard-coding absolute URLs in configuration. A base URL property per environment drifts from the gateway routes; deriving URLs from forwarded headers keeps them right automatically.

6Testing Link Generation

We test at two levels. In each service, a MockMvc test sends the forwarded headers and asserts the Location header, so a service that forgets the strategy fails its own build. At the platform level, an integration test in a staging environment calls a few endpoints of each service through the real gateway and checks that redirects and Location headers start with the public URL.

Java
mockMvc.perform(post("/orders").contentType(APPLICATION_JSON).content(json)
.header("X-Forwarded-Proto", "https")
.header("X-Forwarded-Host", "api.example.com")
.header("X-Forwarded-Prefix", "/shop"))
.andExpect(status().isCreated())
.andExpect(header().string("Location", "https://api.example.com/shop/orders/42")); // passes

MockMvc runs only the filters registered in the test context, so check that ForwardedHeaderFilter is part of it; a @SpringBootTest with @AutoConfigureMockMvc is the safest choice, or the filter can be added to the MockMvc builder explicitly.

7Follow-up questions

Should the gateway rewrite Location headers instead?

Many gateways can rewrite response headers, and it works for simple cases. But it only fixes headers the gateway knows about, not URLs inside response bodies such as HATEOAS links, emails or redirects built in JavaScript. Letting services generate correct URLs from forwarded headers fixes all of them at the source, so header rewriting is at most a temporary patch.

Why not just set the context path to /shop in the service?

Because the service would then be reachable at /shop internally too, coupling it to one gateway route. If the route changes, or two gateways expose it under different prefixes, the service breaks. X-Forwarded-Prefix keeps the service unaware of how it is exposed.

What about X-Forwarded-For for client IPs?

The same trust rule applies: only the gateway's value counts, and the native strategy with a trusted-proxy list is the safest way to turn it into the request's remote address.

8Conclusion

In this answer, we saw that services behind a gateway build URLs from the internal request, so redirects and Location headers leak internal hosts and lose stripped prefixes. Forwarded headers tell services the original scheme, host and prefix, and server.forward-headers-strategy=framework makes Spring apply them through ForwardedHeaderFilter. We trusted the headers only from the gateway by stripping client values and blocking direct access, agreed one convention set by a shared starter, and tested link generation per service and through the gateway.

What to say in the interviewAbout 90 seconds aloud

Open with

The services generate URLs from the request they receive, and behind the gateway that is the internal request: http, the service's internal host, and a path with the prefix already stripped.

Then, if they want more

So Location headers and redirects point to addresses clients cannot use. The fix is for every service to learn the original host, scheme and prefix from forwarded headers. The gateway sends X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port and X-Forwarded-Prefix, and each service sets server.forward-headers-strategy.

I would use framework, which registers ForwardedHeaderFilter and also handles the prefix, so ServletUriComponentsBuilder, redirects, HATEOAS links and Spring Security redirects all produce the public URL without code changes. Trust is the important part: those headers are easy to forge, and a forged host can poison password reset links.

So the gateway must strip any forwarded headers coming from clients and set its own, and services must only be reachable through the gateway, enforced by network policy; with the native strategy, Tomcat's trusted proxy list adds a check.

To make it consistent across 50 services, I would define one header convention, not mixing Forwarded and X-Forwarded, put the property into a shared starter, and keep prefixes only in the gateway routes. Then test it: a MockMvc test per service that sends the headers and checks the Location header, and staging tests through the real gateway.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A gateway in front of 50 MVC services adds and strips path prefixes, and services...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/b54c5beba3/