Quick revision
The short answer
The application builds absolute URLs from the request it sees, and that request comes from the proxy over plain HTTP to the internal host and port, so redirects and HAL links point at http://internal-host:8080. The fix is server.forward-headers-strategy=native or framework so X-Forwarded-Proto, Host, Port and Prefix are applied, a proxy that sends those headers and strips any from clients, no double context path, and tests through the real proxy path.
What to remember
In this answer, we saw that the service builds absolute URLs from the request it receives, which comes from the proxy over plain HTTP to the internal host and port. Setting server.forward-headers-strategy to native or framework applies X-Forwarded-Proto, Host, Port and Prefix, the proxy must set those headers and strip client copies, one place must own the path prefix so it is not applied twice, and redirects and links must be tested through the real proxy.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The application builds absolute URLs from the request it sees, and that request comes from the proxy over plain HTTP to the internal host and port, so redirects and HAL links point at http://internal-host:8080. The fix is server.forward-headers-strategy=native or framework so X-Forwarded-Proto, Host, Port and Prefix are applied, a proxy that sends those headers and strips any from clients, no double context path, and tests through the real proxy path.
Why this question is asked
This is a classic problem after putting a Spring Boot service behind a load balancer or an API gateway: everything works when tested directly, and then users are redirected to an address they cannot reach. Interviewers ask it because the fix is one property, but understanding why it works, and how it can become a security hole, needs a clear picture of what each hop sees. In this answer, we will reproduce the problem, see where the wrong URL comes from, apply the forwarded headers, secure them, deal with the path prefix, and test the result.
1A Quick Recap of What the Application Sees
A user calls https://shop.example.com/orders/login. The load balancer terminates TLS, picks a pod and forwards the request as plain HTTP to http://10.2.3.4:8080/orders/login. From the application's point of view, the request arrived over http, for host 10.2.3.4 or internal-host, on port 8080. It has no idea that the user typed an https URL with a public host name.
Whenever Spring builds an absolute URL, it uses those values. Spring Security's redirect to the login page, a redirect: view, and Spring HATEOAS links in HAL responses, the JSON format with _links that lets clients follow URLs, are all built from the current request. So they all point at the internal address.
5 messages in order. Play them, or step through them one at a time.
The service builds URLs from the hop it sees, not from the URL the user typed.
2Reproducing the Problem
We can reproduce it without the browser by calling through the proxy and printing the response headers and body.
Both outputs show the same three wrong parts: http instead of https, the internal host name instead of shop.example.com, and port 8080 instead of the default 443. The path is right here only because the proxy forwards the same /orders prefix the application uses as its context path.
3Applying the Forwarded Headers
A proxy can tell the application what the original request looked like by adding headers: X-Forwarded-Proto (https), X-Forwarded-Host (shop.example.com), X-Forwarded-Port (443) and X-Forwarded-Prefix (a path prefix the proxy removed), or the standard Forwarded header that combines them. The application must be told to trust and apply them, which is what server.forward-headers-strategy does.
With native, the embedded server rewrites the request's scheme, host, port and remote address before Spring sees it. With framework, Spring's ForwardedHeaderFilter wraps the request so that getScheme(), getServerName() and the URL builders return the forwarded values, and it also understands X-Forwarded-Prefix. Notice that Spring's own URL builders, such as ServletUriComponentsBuilder used by HATEOAS, do not read the headers by themselves; they rely on one of these two mechanisms having applied them. On some platforms, Kubernetes, Cloud Foundry and Heroku among them, Boot defaults to native, but even then it only helps when the proxy really sends the headers and, for Tomcat, its address is trusted, so I prefer to set the strategy explicitly. If the external prefix differs from the context path, check whether your server's native support covers X-Forwarded-Prefix, or use framework.
4Making Sure the Headers Can Be Trusted
Forwarded headers are just request headers, so a client can send them too. If an attacker sends X-Forwarded-Host: evil.example and the proxy passes it through, our redirects and password-reset links could point at the attacker's site. So the proxy must set these headers itself and remove any that arrive from clients, and the application should only trust them from known proxies.
The framework strategy has no notion of trusted addresses, it applies whatever headers arrive, so with it the proxy stripping client values and network rules that stop direct access to the pods are what keep the headers honest.
5Avoiding a Double Context Path
The path needs its own thought. In our setup, the proxy forwards /orders/... unchanged and the application has context path /orders, so they agree. A common alternative is that the proxy strips /orders and forwards /cart to an application with context path /. Then the application must learn the external prefix from X-Forwarded-Prefix, otherwise its links lose /orders.
The bug to avoid is applying the prefix twice: the proxy keeps /orders, the application also has context path /orders, and the proxy additionally sends X-Forwarded-Prefix: /orders. Links then come out as /orders/orders/login. Pick one owner of the prefix, either the context path with the proxy forwarding the path unchanged, or the proxy stripping it and sending X-Forwarded-Prefix to an application at /.
| Proxy forwards | App context path | X-Forwarded-Prefix | Result |
|---|---|---|---|
| /orders/cart | /orders | not sent | Correct links |
| /cart (stripped) | / | /orders | Correct links (framework) |
| /cart (stripped) | / | not sent | Links miss /orders |
| /orders/cart | /orders | /orders | Links get /orders/orders |
6Testing Through the Real Proxy Path
Finally, test the way users arrive. A local test on localhost:8080 never shows this bug, because there is no proxy. We add a check that runs against the staging environment through the real load balancer: request a protected page, assert that the Location header starts with https://shop.example.com/orders/, fetch a HAL resource and assert that every href uses the public base URL, and send a request with a forged X-Forwarded-Host to confirm it is ignored. These checks take seconds and catch the bug whenever the proxy or the properties change.
7Follow-up questions
Which strategy should we choose, native or framework?
Native is efficient and, with Tomcat, can restrict trust to known proxy addresses. Framework works the same on every server and handles X-Forwarded-Prefix in Spring. I usually choose native when the proxy keeps the path, and framework when the proxy strips a prefix, as long as the proxy strips client-sent headers.
Why did this work before we added the load balancer?
Because the browser talked to the application directly, so the request the application saw was the request the user made, with the right scheme, host and port. The proxy introduced a second, different request, and only the forwarded headers carry the original details across it.
Does request.getRemoteAddr() also change?
With the native strategy, yes: the server takes the client address from X-Forwarded-For, which matters for logging and rate limiting. Without it, every request seems to come from the proxy's address.
8Conclusion
In this answer, we saw that the service builds absolute URLs from the request it receives, which comes from the proxy over plain HTTP to the internal host and port. Setting server.forward-headers-strategy to native or framework applies X-Forwarded-Proto, Host, Port and Prefix, the proxy must set those headers and strip client copies, one place must own the path prefix so it is not applied twice, and redirects and links must be tested through the real proxy.
Open with
The application builds absolute URLs, like the login redirect and HAL links, from the request it receives. Behind the load balancer that request is the proxy's request: plain HTTP to internal-host on port 8080.
Then, if they want more
So the app correctly reproduces what it saw, which is the wrong address for users. The original details have to travel in forwarded headers: X-Forwarded-Proto, Host, Port and Prefix, or the standard Forwarded header. I would set server.forward-headers-strategy to native, so Tomcat applies them through its RemoteIpValve, or to framework, so Spring's ForwardedHeaderFilter applies them, which also handles X-Forwarded-Prefix.
Spring's URL builders rely on one of these, they do not read the headers themselves. Then I make sure the proxy actually sends the headers and overwrites any a client sent, because otherwise an attacker can spoof X-Forwarded-Host and get our redirects or reset links to point at their site; with Tomcat I would also restrict trusted proxies.
For the path, one place should own the /orders prefix: either the context path with the proxy passing the path through, or the proxy stripping it and sending X-Forwarded-Prefix, never both, or links become /orders/orders. Finally I would test redirects and HAL links through the real proxy in staging, because localhost never shows this.
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.
- Official documentation
- Spring Boot reference
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 Spring Boot service behind a load balancer is deployed with...": https://scrivio-interview-library.netlify.app/spring-boot/embedded-servers/2c291f93a5/