Proxy 서버에 뒤에 스프링부트를 운영할 때 난감한 상황이 발생합니다. 최초 사용자가 접속하는 환경이 HTTPS 일 경우 헤더에 해당 정보를 담고 있어야 하는데 스프링부트에 설정하지 않으면 X-Forward-* 를 사용하지 않게 됩니다. 스프링 시큐리티의 login / logout 의 redirect ( http 30x )  또는 controller에서 30x 명령을 내리면 프로토콜정보와 포트정보를 사용하지 않고 http://xxxxx:80/xxxx 와 같이 일반적인 URL으로 redirect 요청을 합니다.

[상태설명]

HTTPS[브라우져]  ==> HTTPS[AWS :LB]  ==> HTTP[NginX] ==> Spring Boot
                                                                   |     X-Forward-*    |        ??

이런경우 스프링부트의 [Running Behind a Front-end Proxy Server] 를 참고하여 설정하면 헤더정보를 사용하게 됩니다.

78.12 Running Behind a Front-end Proxy Server

Your application might need to send 302 redirects or render content with absolute links back to itself. When running behind a proxy, the caller wants a link to the proxy and not to the physical address of the machine hosting your app. Typically, such situations are handled through a contract with the proxy, which adds headers to tell the back end how to construct links to itself.

If the proxy adds conventional X-Forwarded-For and X-Forwarded-Proto headers (most proxy servers do so), the absolute links should be rendered correctly, provided server.use-forward-headers is set to true in your application.properties.

[Note]

If your application runs in Cloud Foundry or Heroku, the server.use-forward-headers property defaults to true. In all other instances, it defaults to false.

[Nginx 참고자료]

 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-Host   $host:443;
        proxy_set_header        X-Forwarded-Server $host;
        proxy_set_header        X-Forwarded-Port   443;
        proxy_set_header        X-Forwarded-Proto  https;
        proxy_set_header        X-HTTPS-Protocol $ssl_protocol;

        proxy_redirect     off;
        proxy_pass http://localhost:8080;

Leave a Reply

Your email address will not be published. Required fields are marked *