Thymeleaf template (in Spring boot application) behind reverse proxy not forming url's correctly
Correct solution is to inform springboot application that it is being behind proxy.
Try to extend nginx config to something like this:
location /app {
proxy_pass http://10.0.0.0:8080;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Prefix /app;
}
To learn about using Forwarded header (standard for a few years), have a look at https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/ There are a bit mismatch in spring documentation for this topic and you can have a look at some issues/project to gain more info:
- "plugin" which helps a lot https://github.com/qaware/x-forwarded-filter
- https://github.com/spring-projects/spring-boot/issues/15046
- https://github.com/spring-projects/spring-hateoas/issues/862
- https://github.com/spring-projects/spring-security/issues/7081
PS: If you are testing your springboot app configuration, it can come handy to do it locally with cURL, without need to use nginx. To simulate request headers like comming from proxy, you can use this command:
curl -i http://localhost:8080 \
-H 'X-Forwarded-Host: example.com' \
-H 'X-Forwarded-Port: 443' \
-H 'X-Forwarded-prefix: /myDevelApp' \
-H 'X-Forwarded-proto: https'
Or using standardized header:
curl -i http://localhost:8080 \
-H 'X-Forwarded-prefix: /myDevelApp' \
-H 'Forwarded: for=123.333.333.333;host=example.com;proto=https'