Linux,Unix,BSD

HAProxy http -> https로 전달 설정하기

채윤아빠 2021. 8. 7. 15:18
728x90
반응형

개요

웹서버의 보안을 위하여 https를 이용하는 것이 좋습니다.
무료로 Let's Encrypt의 SSL 인증서를 발급 받아서 웹서버에 적용하면, 조금 더 쉽게 무료로 보안성 있는 서비스가 가능합니다.

본 문서에서는 이런한 HTTPS 적용하는 방법에 있어서, HAProxy를 이용하여 웹서버 앞단에서 Load balance의 역할을 주었을 때, HAProxy 단에서 HTTP를 HTTPS로 전달(redirect)하는 방법에 대해서 살펴보겠습니다.

HAProxy 설정

전달 (redirect) 설정

HTTP를 HTTPS로 전달하기 위해서는 haproxy.cfg에 다음과 같은 설정을 추가합니다.

http-request set-header X-Forwarded-Proto https if { ssl_fc }
http-request set-header X-Forwarded-Proto http if !{ ssl_fc }

http 설정단에 아래와 같이 추가하면 기본적으로 https로 바로 전달됩니다.

http-request redirect scheme https unless { ssl_fc }

하지만 위와 같이 설정한 경우, 응답코드 (response code)가 302로 임시적으로 이동하게 (Temporary Redirect) 됩니다.

아래와 같이 응답코드 (response code)를 301로 설정하여 영구적으로 이동하게 (Moved Permanently) 설정할 수 있습니다.

http-request redirect scheme https code 301 unless { ssl_fc }

HTTP Strict Transport Security (HSTS) 설정

    # max-age is mandatory 
    # 16000000 seconds is a bit more than 6 months
    http-response set-header Strict-Transport-Security "max-age=16000000; includeSubDomains; preload;"

참고자료

"Redirect HTTP to HTTPS with HAProxy":https://www.haproxy.com/blog/redirect-http-to-https-with-haproxy/
"HTTPS를 강제하는 HSTS 사용하기":https://brunch.co.kr/@sangjinkang/40
"Redirect HTTP to HTTPS with HAProxy":https://www.haproxy.com/blog/redirect-http-to-https-with-haproxy/