Wim Deblauwe

Using ServletUriComponentsBuilder with Nginx

March 13, 2017 · 1 min read

Spring’s ServletUriComponentsBuilder.fromCurrentRequest() can be really convinient to build an URL from the current request. However, when using Nginx, things might work so well.

The solution is simple, just add these headers to your Nginx configuration file:

proxy_set_header        X-Forwarded-Host   $host:443;
proxy_set_header        X-Forwarded-Server $host;
proxy_set_header        X-Forwarded-Port   443;
proxy_set_header        X-Forwarded-Proto  https;

UPDATE 2020-06-08

You need to enable support for this in Spring Boot by setting the following property (Since Spring Boot 2.2):

server.forward-headers-strategy=native