在前一段時間,我寫了幾篇有關學習haproxy的文章。今天我們再來介紹下haproxy的https配置,https協定的好處在此,我們就不就作介紹了。
我們隻介紹如何配置https,以及https在實際生産環境中的應用。
<b>ps</b><b>:本實驗全部在haproxy1.5.4版本進行測試通過。haproxy1.3版本以下haproxy配置參數可能不能使用,需要注意版本号。</b>
<b>以下haproxy配置是線上生産環境直接使用的。</b>
現在根據業務的實際需要,有以下幾種不同的需求。如下:
把所有請求http://http.ilanni.com的位址全部跳轉為https//:http.ilanni.com這個位址。
伺服器同時開放http://http.ilanni.com和https://http.ilanni.com的通路形式。
同一台伺服器對http.ilanni.com域名通路的全部跳轉為https://http.ilanni.com,而對haproxy.ilanni.com通路走http協定,也就是跳轉到http://haproxy.ilanni.com這個位址。
同一台伺服器對http.ilanni.com和haproxy.ilanni.com通路走http是協定。
現在我們根據業務的需求,我們來配置haproxy一一達到其需求。
說實話haproxy的https配置要比nginx配置簡單的多了,我們隻需要加入幾行代碼即可實作https的功能。
http跳轉https的haproxy配置檔案内容,如下:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 188
gid 188
daemon
<b>tune.ssl.default-dh-param 2048</b>
defaults
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.1
option redispatch
retries 3
maxconn 2000
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
listen admin_stats
bind 0.0.0.0:1080
maxconn 10
stats refresh 30s
stats uri /stats
stats auth admin:admin
stats hide-version
frontend weblb
bind *:80
acl is_http hdr_beg(host) http.ilanni.com
<b>redirect scheme https if !{ ssl_fc }</b>
<b>bind *:443 ssl crt /etc/haproxy/ilanni.com.pem</b>
use_backend httpserver if is_http
backend httpserver
balance source
server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

在以上配置檔案中,需要注意的選項如下:
tune.ssl.default-dh-param 2048因為我們的ssl密鑰使用的是2048bit加密,是以在此進行聲明。
redirect scheme https if !{ ssl_fc }
bind *:443 ssl crt /etc/haproxy/ilanni.com.pem
這三行表示把所有通路http.ilanni.com這個域名的請求,全部轉發到https://http.ilanni.com這個連接配接。
http跳轉https配置完畢後,我們選擇來測試其跳轉。如下:
你會發現在浏覽器中,無論你輸入的是http.ilanni.com,還是http://http.ilanni.com亦或是https://http.ilanni.com,都會自動跳轉到https://http.ilanni.com。
這樣就達到了,把所有的http請求跳轉到https的目的。
haproxy要實作http和https并存的話,配置也很簡單,隻需要把haproxy分别監控不同的端口就行,配置檔案如下:
user haproxy
group haproxy
tune.ssl.default-dh-param 2048
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend weblb443
acl is_443 hdr_beg(host) http.ilanni.com
use_backend httpserver443 if is_443
backend httpserver443
在以上配置檔案中,我們定義了兩個前端,一個前端用于監聽80端口,也就是http協定。另外一個前端監聽443端口,也就是https協定。
此時haproxy會根據用戶端請求的協定進行分發,如果發現用戶端請求的是http協定,則把該請求分發到監聽80端口的前端。如果發現用戶端請求的是https協定,則把該請求分發到監聽443端口的前端。如此就達到了haproxy讓http和https并存的要求。
http與https并存配置完畢後,我們選擇來測試其跳轉。如下:
通過測試你會發現,在浏覽器中如果你輸入的是http://http.ilanni.com或者是http.ilanni.com都會直接跳轉到http://http.ilanni.com,而輸入的是https://http.ilanni.com,則隻會跳轉到https://http.ilanni.com。
如此就到達了,我們業務的要求實作http和https并存。
同台伺服器不同域名之間的http和https配置比較複雜,第一需要監聽兩個端口,第二還要根據不同的域名進行分發。
haproxy配置檔案如下:
acl is_haproxy hdr_beg(host) haproxy.ilanni.com
redirect prefix https://http.ilanni.com if is_http
use_backend haproxyserver if is_haproxy
backend haproxyserver
server web1 127.0.0.1:9090 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
同台伺服器不同域名之間的https與http配置,我們配置了兩個前端一個用于監聽80端口,并且根據不同的域名進行跳轉。在80端口的規則中,如果用戶端請求的是http.ilanni.com,這個域名的話,則haproxy會把該請求直接跳轉到https://http.ilanni.com。如果是haproxy.ilanni.com,這個域名的話,則分發到後端的伺服器。
另外一個前端用于監聽443端口,用于分發用戶端https://http.ilanni.com的請求。
同台伺服器不同域名之間的https與http配置配置完畢後,我們現在來進行測試。如下:
通過上圖,我們可以發現在浏覽器中輸入haproxy.ilanni.com會跳轉到http://haproxy.ilanni.com這個位址,而如果輸入的是http.ilanni.com或者是http://http.ilanni.com,亦或是https://http.ilanni.com的話,都會跳轉到https://http.ilanni.com。
如此就達到了我們的業務要求,同台伺服器上通路haproxy.ilanni.com直接跳轉到80端口,如果通路的是http.ilanni.com域名的話則跳轉到https://http.ilanni.com這個位址。
要使同台伺服器的兩個設定多個域名都使用https協定的話,配置很簡單。隻需要在haproxy中啟用各自的https配置即可。
haproxy配置檔案,如下:
uid 108
gid 116
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.1
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s
stats uri /stats
stats auth admin:admin
stats hide-version
frontend web80
bind *:80
acl is_http hdr_beg(host) http.ilanni.com
redirect scheme https if !{ ssl_fc }
bind *:443 ssl crt /etc/haproxy/ilanni.com.pem
acl is_haproxy hdr_beg(host) haproxy.ilanni.com
use_backend httpserver if is_http
use_backend haproxyserver if is_haproxy
balance source
server web1 127.0.0.1:6060 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
配置檔案比較簡單,在此就不做進一步的講解了。
同台伺服器多域名均使用https,配置完畢後,現在我們來測試下。
通過上圖,我們可以看到在浏覽中無論是輸入http.ilanni.com、http://http.ilanni.com,還是haproxy.ilanni.com、http://haproxy.ilanni.com,都會跳轉到相應的https位址。
這也達到了我們業務的要求。