Configuring Spring Boot 3 Behind Nginx Reverse Proxy
Recently got a very unique usecase where nginx is redirecting request to a different port that is provided by Nginx. Based on below image, we can see that user is accessing port 8080 on Nginx but getting HTTP code 302 redirect to port 8081 which is not exposed by Nginx.
Workaround is quite straighforward, we can set this configuration on Spring Boot’s application properties.
server.forward-headers-strategy=FRAMEWORK
while having this configuration on nginx.conf
location / {
proxy_pass http://127.0.0.1:8081;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;
proxy_busy_buffers_size 512k;
proxy_buffers 8 512k;
proxy_buffer_size 256k;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
client_max_body_size 50M;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
proxy_ssl_verify off;
proxy_set_header cookie $http_cookie;
port_in_redirect off;
absolute_redirect off;
}
We can use this Java project for testing,
https://github.com/edwin/spring-3-keycloak
