haproxy代理https配置方法【轉】


記得在之前的一篇文章中介紹了nginx反向代理https的方法,今天這里介紹下haproxy代理https的方法:

haproxy代理https有兩種方式:
1)haproxy服務器本身提供ssl證書,后面的web服務器走正常的http
2)haproxy服務器本身只提供代理,后面的web服務器走https(配置ssl證書)


第一種方式:haproxy服務器本身提供ssl證書

注意:
需要編譯haproxy的時候支持ssl
編譯參數:
#make TARGET=linux26 USE_OPENSSL=1 ADDLIB=-lz
#ldd haproxy | grep ssl
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fb0485e5000)

 

 

配置參數(修改haproxy.cfg文件)

frontend https_frontend
   bind *:443 ssl crt /etc/ssl/certs/servername.pem
   mode http
   option httpclose
   option forwardfor
   reqadd X-Forwarded-Proto:\ https
   default_backend web_server

backend web_server
  mode http
  balance roundrobin
  cookie SERVERID insert indirect nocache
  server s1 192.168.1.150:80 check cookie s1
  server s2 192.168.1.151:80 check cookie s2

-----------------------------------------------------------
注意:這里的pem 文件是下面兩個文件合並而成:
#cat servername.crt servername.key |tee servername.pem
-----------------------------------------------------------

 

第二種方式:haproxy服務器本身只提供代理,沒有ssl證書 (一般我們常用的就是這種方式)

這種方式,haproxy不需要重新編譯支持ssl,簡單方便,只需要后面的web服務器配置好ssl即可。

配置參數(修改haproxy.cfg文件)


frontend https_frontend
  bind *:443
  mode tcp
  default_backend web_server

backend web_server
  mode tcp
balance roundrobin
  stick-table type ip size 200k expire 30m
  stick on src
  server s1 192.168.1.150:443
  server s2 192.168.1.151:443

---------------------------------------------------------
注意,這種模式下mode 必須是tcp 模式
---------------------------------------------------------

 

本文由ilanniweb提供友情贊助,首發於爛泥行天下

想要獲得更多的文章,可以關注我的微信ilanniweb。

在前一段時間,我寫了幾篇有關學習haproxy的文章。今天我們再來介紹下haproxy的https配置,https協議的好處在此,我們就不就作介紹了。

我們只介紹如何配置https,以及https在實際生產環境中的應用。

PS:本實驗全部在haproxy1.5.4版本進行測試通過。haproxy1.3版本以下haproxy配置參數可能不能使用,需要注意版本號。

以下haproxy配置是線上生產環境直接使用的。

一、業務要求

現在根據業務的實際需要,有以下幾種不同的需求。如下:

1.1 http跳轉https

把所有請求http://http.ilanni.com的地址全部跳轉為https//:http.ilanni.com這個地址。

1.2 http與https並存

服務器同時開放http://http.ilanni.com和https://http.ilanni.com的訪問形式。

1.3 同台服務器不同域名之間的https與http

同一台服務器對http.ilanni.com域名訪問的全部跳轉為https://http.ilanni.com,而對haproxy.ilanni.com訪問走http協議,也就是跳轉到http://haproxy.ilanni.com這個地址。

1.4 同台服務器多域名均使用https

同一台服務器對http.ilanni.com和haproxy.ilanni.com訪問走http是協議。

二、配置haproxy並測試業務需求

現在我們根據業務的需求,我們來配置haproxy一一達到其需求。

2.1 http跳轉https配置

說實話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

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.1

option redispatch

retries 3

option redispatch

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

maxconn 3000

listen admin_stats

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 weblb

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

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

clip_image001[4]

在以上配置文件中,需要注意的選項如下:

tune.ssl.default-dh-param 2048因為我們的SSL密鑰使用的是2048bit加密,所以在此進行聲明。

acl is_http hdr_beg(host) http.ilanni.com

redirect scheme https if !{ ssl_fc }

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

這三行表示把所有訪問http.ilanni.com這個域名的請求,全部轉發到https://http.ilanni.com這個連接。

2.2 測試http跳轉https

http跳轉https配置完畢后,我們選擇來測試其跳轉。如下:

clip_image002[10]

你會發現在瀏覽器中,無論你輸入的是http.ilanni.com,還是http://http.ilanni.com亦或是https://http.ilanni.com,都會自動跳轉到https://http.ilanni.com。

這樣就達到了,把所有的http請求跳轉到https的目的。

2.3 http與https並存配置

haproxy要實現http和https並存的話,配置也很簡單,只需要把haproxy分別監控不同的端口就行,配置文件如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

user haproxy

group haproxy

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

retries 3

option redispatch

maxconn 2000

timeout connect 5000ms

timeout client 50000ms

timeout server 50000ms

listen admin_stats

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 weblb

bind *:80

acl is_http hdr_beg(host) http.ilanni.com

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

frontend weblb443

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

acl is_443 hdr_beg(host) http.ilanni.com

use_backend httpserver443 if is_443

backend httpserver443

balance source

server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

在以上配置文件中,我們定義了兩個前端,一個前端用於監聽80端口,也就是http協議。另外一個前端監聽443端口,也就是https協議。

此時haproxy會根據客戶端請求的協議進行分發,如果發現客戶端請求的是http協議,則把該請求分發到監聽80端口的前端。如果發現客戶端請求的是https協議,則把該請求分發到監聽443端口的前端。如此就達到了haproxy讓http和https並存的要求。

2.4 測試http與https並存

http與https並存配置完畢后,我們選擇來測試其跳轉。如下:

clip_image003[4]

clip_image002[11]

通過測試你會發現,在瀏覽器中如果你輸入的是http://http.ilanni.com或者是http.ilanni.com都會直接跳轉到http://http.ilanni.com,而輸入的是https://http.ilanni.com,則只會跳轉到https://http.ilanni.com。

如此就到達了,我們業務的要求實現http和https並存。

2.5 同台服務器不同域名之間的https與http配置

同台服務器不同域名之間的http和https配置比較復雜,第一需要監聽兩個端口,第二還要根據不同的域名進行分發。

haproxy配置文件如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

uid 188

gid 188

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.1

option redispatch

retries 3

option redispatch

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

maxconn 3000

listen admin_stats

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 weblb

bind *:80

acl is_haproxy hdr_beg(host) haproxy.ilanni.com

acl is_http hdr_beg(host) http.ilanni.com

redirect prefix https://http.ilanni.com if is_http

use_backend haproxyserver if is_haproxy

backend haproxyserver

balance source

server web1 127.0.0.1:9090 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

frontend weblb443

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

acl is_443 hdr_beg(host) http.ilanni.com

use_backend httpserver443 if is_443

backend httpserver443

balance source

server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

clip_image004[4]

同台服務器不同域名之間的https與http配置,我們配置了兩個前端一個用於監聽80端口,並且根據不同的域名進行跳轉。在80端口的規則中,如果客戶端請求的是http.ilanni.com,這個域名的話,則haproxy會把該請求直接跳轉到https://http.ilanni.com。如果是haproxy.ilanni.com,這個域名的話,則分發到后端的服務器。

另外一個前端用於監聽443端口,用於分發客戶端https://http.ilanni.com的請求。

2.6 測試同台服務器不同域名之間的https與http配置

同台服務器不同域名之間的https與http配置配置完畢后,我們現在來進行測試。如下:

clip_image005[4]

clip_image002[12]

通過上圖,我們可以發現在瀏覽器中輸入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這個地址。

2.7 同台服務器多域名均使用https配置

要使同台服務器的兩個設置多個域名都使用https協議的話,配置很簡單。只需要在haproxy中啟用各自的https配置即可。

haproxy配置文件,如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

uid 108

gid 116

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.1

option redispatch

retries 3

option redispatch

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

listen admin_stats

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

redirect scheme https if !{ ssl_fc }

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

use_backend httpserver if is_http

use_backend haproxyserver if is_haproxy

backend httpserver

balance source

server web1 127.0.0.1:6060 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend haproxyserver

balance source

server web1 127.0.0.1:9090 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

clip_image006[4]

配置文件比較簡單,在此就不做進一步的講解了。

2.8 測試同台服務器多域名均使用https

同台服務器多域名均使用https,配置完畢后,現在我們來測試下。

clip_image002[13]

clip_image007[4]

通過上圖,我們可以看到在瀏覽中無論是輸入http.ilanni.com、http://http.ilanni.com,還是haproxy.ilanni.com、http://haproxy.ilanni.com,都會跳轉到相應的https地址。

這也達到了我們業務的要求

 

 

轉自

haproxy代理https配置方法 http://www.mamicode.com/info-detail-1539510.html

爛泥:haproxy學習之https配置 http://www.cnblogs.com/ilanni/p/4941056.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM