The labs on this site need JavaScript to run. The same content is written out below.
Learn › Networking › In front of the web server
Proxies and reverse proxies
How does Spring Boot behind nginx know who sent the request?
Why it exists — If an application server faces the internet directly, every server needs its own certificate and has to handle HTTPS, and when you grow to several servers, there is no separate place to spread the requests among them. A reverse proxy accepts outside connections in one place, decrypts the HTTPS, and passes the requests on to the servers behind it, so certificate management and request distribution are gathered in one place in front of the servers.
Where it's used today — A common setup puts nginx or a cloud load balancer in front of a Spring Boot server to handle HTTPS and to spread requests. nginx adds X-Forwarded-For and X-Forwarded-Proto with proxy_set_header, and Spring Boot reads those headers with server.forward-headers-strategy in application.properties. Then the code that writes access logs, limits the number of requests per address, or builds an https:// redirect address gets the original client's values just by calling request.getRemoteAddr() and request.getScheme().
A proxy is an intermediate server that receives requests on behalf of someone and passes them on somewhere else. A proxy that stands on the client side and acts for the client is a forward proxy, and a proxy that stands on the server side and acts for the server is a reverse proxy. This lab puts nginx, the most common reverse proxy, in front of a Spring Boot server. It is nginx that actually opens the TCP connection to the server, so the peer that Tomcat sees is the address of nginx. That is why nginx passes the original address and protocol in the X-Forwarded-For and X-Forwarded-Proto headers, and Spring Boot reads those headers according to the server.forward-headers-strategy setting and changes the values of request.getRemoteAddr() and request.getScheme(). The trap is that a client can also write these headers with any value it likes.
Checked against real execution · Checked against real execution: the nginx configuration on the screen was put into real nginx 1.27.5 (the nginx:1.27-alpine image) (only the omitted certificate lines were filled in), the controller on the screen was compiled as it is and started as real Spring Boot 4.1.1 (embedded Tomcat 11.0.24) with none, native, framework, and with the setting not written, and 32 combinations were run in Docker with the addresses on the screen as they are (client 203.0.113.7, nginx 10.0.0.5) and the five columns were checked (internal-proxies at its default, verify/net/cmp_revproxy.js)
Concepts in this lab
- Reverse proxy
- Why it exists — If slow clients connect straight to an application server, the request processing threads stay tied up with those connections while the clients slowly upload a large request body or slowly take a response. A reverse proxy such as Nginx, in its default configuration, buffers requests and responses and exchanges data quickly with the backend server, so the backend server's threads are not tied up for long by slow connections.
- Where it's used today — It is used to split static file requests and /api requests by path and send them to different servers, or to reject too-large uploads at the front with Nginx's client_max_body_size. In Kubernetes, an Ingress controller takes this role.
- A server that receives client requests on behalf of internal servers, forwards them to those servers, and returns their responses to the client. The client does not know the actual servers directly. Its roles include distributing requests across several servers, handling TLS, caching, and protecting the internal servers.
- Forward proxy
- Where it's used today — In a company network where you must go through a proxy to get outside, you must write the proxy address in Maven's settings.xml or Gradle's gradle.properties to download libraries. A Java program sets it with system properties such as https.proxyHost.
- A forward proxy is an intermediate server that stands on the client side and sends the client's requests on to outside servers on its behalf. The client sets the address of the proxy and goes through it on purpose, and the peer that a server sees at the other end of the connection is the proxy, not the client. It is used to collect and log or block the requests that go out from a company or a school in one place, or to hide the address of the client. It is the opposite of a reverse proxy, which stands on the server side and acts for the server, in terms of whom it acts for.
- X-Forwarded-For
- Where it's used today — You read this value to record the real user IP in an access log, or to block requests that come too often from one IP. In nginx, the proxy_set_header setting attaches this header and passes it on.
- X-Forwarded-For is a request header in which a proxy writes the address of the original client when it passes a request on. The peer that a server behind a proxy sees is the proxy, so the address of the real client is passed on only through this header. When a request goes through several proxies, each proxy appends the address of whoever connected to it at the end, separated by a comma. It is a request header, so a client can also write any value into it; a server must use only the values added by a trusted proxy that it manages (the values on the right), and must not read it at all if it is not behind a proxy. It is a widely used convention, not a standard, and the standard header that does the same job is Forwarded.
- X-Forwarded-Proto and TLS termination
- Where it's used today — It is used in a setup where nginx or a cloud load balancer receives HTTPS and the Spring Boot server behind receives only HTTP. For the address that Spring Security redirects to the login page to go out as https://, you make it read this header with server.forward-headers-strategy.
- TLS termination is a setup in which a reverse proxy or a load balancer receives the HTTPS connection of the client, removes the encryption, and passes the request on to the server behind it as unencrypted HTTP. Certificate management and encryption work can be gathered in one place. In this setup the server behind sees only an HTTP connection, so the proxy tells it the protocol the client originally used with the X-Forwarded-Proto: https header. If the server does not read this header, it may build redirect addresses with http:// or misjudge the conditions for secure cookies. Like X-Forwarded-For, use only the values added by a trusted proxy.
- server.forward-headers-strategy
- Why it exists — When a server stands behind nginx or a load balancer and the TCP connection peer is always the proxy, Spring Boot bundled the work of restoring the original address and whether it was https from headers into one setting, so that each application does not have to write it by hand.
- Where it's used today — You write server.forward-headers-strategy=native in Spring Boot behind nginx, so that access logs and request limits use the real user IP and the login redirect address of Spring Security goes out as https://.
- server.forward-headers-strategy is an application.properties setting that decides who reads the X-Forwarded-* headers that a proxy adds (or the standard Forwarded header) in Spring Boot. native has the web server read them directly, which registers the RemoteIpValve if it is Tomcat, framework has the ForwardedHeaderFilter of Spring read them, and none means nobody reads them. If they are read, request.getRemoteAddr() returns the address of the original client and request.getScheme() returns the original protocol (https). If you do not set it, it is native on a cloud platform such as Kubernetes and none elsewhere. The setting that makes it read the headers and the setting in which the proxy adds or rewrites the headers must be paired.
- RemoteIpValve and internal-proxies
- Why it exists — Anyone can write a header, so it was created to judge who added a value by the address of the connection peer, and to use only the values added by a trusted proxy.
- Where it's used today — If the address of nginx or the load balancer is outside the default private address ranges, write that address in server.tomcat.remoteip.internal-proxies. Do not leave it empty on a production server so as to trust every proxy.
- RemoteIpValve is a valve that Tomcat provides (a Tomcat component that processes a request before the controller), and in Spring Boot it is registered when server.forward-headers-strategy=native. Only when the TCP connection peer is in the list of trusted proxies (server.tomcat.remoteip.internal-proxies) does it read X-Forwarded-For and X-Forwarded-Proto, and it reads X-Forwarded-For from the back and sets the first address that is not a trusted proxy as the value of getRemoteAddr(). So in a setup where the proxy appends after the received value, it is not fooled by a fake address the client wrote at the front. The default of internal-proxies in Spring Boot 4.1 is private and loopback address ranges such as 10.0.0.0/8, 192.168.0.0/16, and 127.0.0.0/8. If the connection peer is not a trusted proxy, it ignores the headers.
- ForwardedHeaderFilter
- Why it exists — Instead of a different way for each web server, it was created so that Spring handles Forwarded (RFC 7239) and the X-Forwarded-* headers in one way, and so that the code that builds links sees the original address.
- Where it's used today — You choose framework when the default handling of the web server is not enough (for example when you must also change the front part of the path with X-Forwarded-Prefix). In that case, have nginx write X-Forwarded-For anew with $remote_addr.
- ForwardedHeaderFilter is a servlet filter that Spring Framework provides, and in Spring Boot it is registered when server.forward-headers-strategy=framework. It receives the request before the controller, passes it on after replacing it with a request object that returns the values of the Forwarded or X-Forwarded-* headers, and removes those headers. For X-Forwarded-For it uses the first value as the value of getRemoteAddr(), and it does not check whether the connection peer is a trusted proxy. So the Spring documentation says it is safe only if the proxy that touches the outside removes or rewrites these headers.
- HTTPS and TLS
- HTTPS is HTTP communication encrypted with TLS. TLS keeps third parties from seeing the content (confidentiality), keeps it from being changed on the way (integrity), and uses a certificate to check that the server you connected to is the real one (authentication). SSL is the protocol that came before TLS. It is now deprecated and only TLS is actually used, but the name SSL is still used alongside it by habit.
Try these
- Default settings — Tomcat reads X-Forwarded-For from the back and picks the value nginx added.
- If you do not set it — Every request looks as if it came from the address of the one nginx, over http.
- Reading with framework — The ForwardedHeaderFilter uses the first value of X-Forwarded-For.
- If nginx writes a new value — The value the client wrote disappears at nginx.
- If nginx has no header lines — nginx passes the received header on as it is, so Tomcat receives only the value the client wrote.
- Turning on framework without a proxy — The ForwardedHeaderFilter does not check who the connection peer is.
- Splitting it yourself — Doing by hand what is done inside makes it easy to trust the forged value at the front.
Running with the default settings
- The client (203.0.113.7) requests https://shop.example/whoami. What shop.example points to is nginx (10.0.0.5). The client takes nginx to be the server, and does not know that a Spring Boot server (app) is separately behind it. This client wrote an X-Forwarded-For: 198.51.100.99 header into the request itself. A request header is text that the sender can fill in however it likes.
- nginx decrypts the HTTPS, follows proxy_pass http://app:8080 to make a new TCP connection to the Spring Boot server on the internal network, and passes the request on as HTTP. Following the line proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for, it appends the address of the peer that connected to it ($remote_addr, 203.0.113.7) after the received X-Forwarded-For, joined with a comma. So the value the client wrote comes first, and the value nginx added comes after it. The line X-Forwarded-Proto $scheme also writes the protocol the client used (https), and the line Host $host passes on the name the client requested (shop.example) as it is.
- Tomcat receives the request. The TCP connection peer is 10.0.0.5, the address of nginx, because it is nginx that made the connection with Tomcat, and the connection it received is plain HTTP, not encrypted. The X-Forwarded-For that arrived is "198.51.100.99, 203.0.113.7", and X-Forwarded-Proto is https.
- The RemoteIpValve of Tomcat, before passing the request to the controller, checks the connection peer (10.0.0.5) against server.tomcat.remoteip.internal-proxies. It is an address in the default (private address ranges such as 10.0.0.0/8), so it is treated as a trusted proxy and the headers are read. It reads X-Forwarded-For from the back, and takes the first address that is not a trusted proxy (203.0.113.7) as the value of getRemoteAddr(). It does not look at the values before it. With X-Forwarded-Proto: https, getScheme() becomes https.
- request.getRemoteAddr() is 203.0.113.7, the address of the real client. request.getScheme() is https, the same as the protocol the user used.
Result of this run
- native has the RemoteIpValve of Tomcat read the headers. It looks at the headers only when the connection peer is a trusted proxy (internal-proxies), and it reads X-Forwarded-For from the back and picks the first address that is not a trusted proxy. The last value was added directly by nginx, so it is not fooled however much the client writes in front of it.
Code
# nginx configuration (receives HTTPS on port 443 of shop.example and passes it to the app server)
server {
listen 443 ssl;
server_name shop.example;
# … certificate (ssl_certificate) settings omitted
location / {
proxy_pass http://app:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# application.properties (Spring Boot)
# The RemoteIpValve of Tomcat reads the X-Forwarded-* headers
# The proxy addresses it trusts are in server.tomcat.remoteip.internal-proxies (the default includes private address ranges such as 10.0.0.0/8)
server.forward-headers-strategy=native
// WhoAmIController.java (WhoAmIController is a controller made for this example. HttpServletRequest and getRemoteAddr() and getScheme() come from the servlet API)
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WhoAmIController {
@GetMapping("/whoami")
public String whoami(HttpServletRequest request) {
return request.getRemoteAddr() + " " + request.getScheme();
}
}
Assumptions of this model
All 32 combinations were run with real nginx 1.27.5 in front and real Spring Boot 4.1.1 (embedded Tomcat 11.0.24, Spring Framework 7.0.9) behind it. The nginx configuration on the screen (the server block) was put as it is into the http block of nginx.conf, and the one change is that the two lines ssl_certificate and ssl_certificate_key were put where the screen omits the certificate settings (a test certificate made with openssl, with the name shop.example). The additions are the frame that the screen does not show (the events block and the braces of the http block) and the lines that keep records (error_log, log_format, access_log). For Spring Boot, the text of the WhoAmIController on the screen was compiled as it is, and the application.properties line on the screen (server.forward-headers-strategy=none, native, framework) was written out to a file as it is and the server was started (the controller that splits by hand with none). The headers that arrived at Tomcat and the values the controller returned were recorded, and all five columns were confirmed to be the same as the model (verify/net/cmp_revproxy.js, match 32 / 32). The headers that arrived at Tomcat were read on the server started with none. A server with the setting not written at all returned the same values as none, and it was also confirmed that the RemoteIpValve is registered in Tomcat only with native, and the ForwardedHeaderFilter only with framework. The addresses are the same on the screen and in the run. Two networks were created in Docker 29.8.1 on the PC used to write this lab. The outside network is the documentation address range 203.0.113.0/24, and the internal network is the private address range 10.0.0.0/24. The client sent requests with curl 8.12.1 from a container that is 203.0.113.7 on the outside network; nginx (the nginx:1.27-alpine image) is a container that has both 10.0.0.5 on the internal network and an address on the outside network; and Spring Boot (Java 21.0.13 of the eclipse-temurin:21-jre image) is a container named app on the internal network (10.0.0.8). shop.example on the client was made to point to 10.0.0.5, and the route to that address was set to the outside address of the nginx container. So the client connected to port 443 of 10.0.0.5 with HTTPS (TLS 1.3) and verified the test certificate, the peer that nginx saw ($remote_addr) was 203.0.113.7, and the peer that Tomcat saw was 10.0.0.5. In the combinations that connect directly to Spring Boot, nginx was removed and the Spring Boot container was also attached to the outside network, and the client connected directly to its port 8080. server.tomcat.remoteip.internal-proxies was not set, and the run used the default of Spring Boot 4.1.1. The default is a list of private and loopback address ranges such as 10.0.0.0/8, 192.168.0.0/16, and 127.0.0.0/8, so it trusts 10.0.0.5 and does not trust 203.0.113.7 and 198.51.100.99. nginx 1.27.5 passed the request to the back end with HTTP/1.0 and Connection: close, and in the configuration with no proxy_set_header lines it changed Host to app:8080 ($proxy_host) and passed on the other request headers, such as X-Forwarded-For, as they are. That $proxy_add_x_forwarded_for is the received X-Forwarded-For followed by $remote_addr joined with a comma, and equal to $remote_addr if there is no received header, was also confirmed with the access_log of nginx and the headers that arrived at Tomcat. The addresses on the screen (203.0.113.7 and 198.51.100.99 are for documentation examples, 10.0.0.x is a private network) are not addresses on the real internet, and the client, nginx, and Spring Boot in this run are not different machines but different containers on the same PC. When Spring Boot recognizes a cloud platform such as Kubernetes, it changes the default of this setting to native, and this lab covers only the case where it does not. X-Forwarded-For and X-Forwarded-Proto are not standards but widely used conventions, and the standard header that conveys the same information is Forwarded (RFC 7239). Forward proxies, the case where several proxies are chained, and load balancer distribution are not covered.
Previous lab: Load balancers and scale out · Next lab: CDN