配置HAProxy支持https協議


author:JevonWei
版權聲明:原創作品


實現http重定向到https

HAProxy

創建CA證書
[root@HAProxy ~]# cd /etc/haproxy/
[root@HAProxy haproxy]# mkdir certs
[root@HAProxy haproxy]# cd /etc/pki/CA
[root@HAProxy CA]# (umask 077;openssl genrsa -out private/cakey.pem 4096)
[root@HAProxy CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365 \\自簽名證書
[root@HAProxy CA]# touch index.txt
[root@HAProxy CA]# echo 01 > serial

[root@HAProxy CA]# cd /etc/haproxy/certs/
[root@HAProxy certs]# openssl genrsa -out haproxy.key 2048
[root@HAProxy certs]# openssl req -new -key haproxy.key -out haproxy.csr
[root@HAProxy certs]# [root@HAProxy certs]# openssl ca -in haproxy.csr -out haproxy.crt
[root@HAProxy certs]# ls
haproxy.crt  haproxy.csr  haproxy.key
[root@HAProxy certs]# cat haproxy.crt haproxy.key > haproxy.pem
[root@HAProxy certs]# ll
total 12
-rw-r--r--. 1 root root    0 Aug 29 19:36 haproxy.crt
-rw-r--r--. 1 root root 1009 Aug 29 19:35 haproxy.csr
-rw-r--r--. 1 root root 1675 Aug 29 19:34 haproxy.key
-rw-r--r--. 1 root root 1675 Aug 29 19:37 haproxy.pem
[root@HAProxy certs]# chmod 600 haproxy.pem 

配置haproxy網頁跳轉

[root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
    frontend https *:443
        bind *:443 ssl crt /etc/haproxy/certs/haproxy.pem
        acl static path_end .jpg .jpeg .png .gif .txt .html
        acl static path_beg /imgs /images /css
        use_backend staticsrvs  if static
        default_backend dynsrvs
    frontend http
        bind *:8080
        redirect scheme https if !{ ssl_fc }
    backend dynsrvs
        balance roundrobin
        server dynsrv1 172.16.253.105:80 check
        server dynsrv2 172.16.253.105:8080 check
    backend staticsrvs
        balance roundrobin
        server staticsrv1 172.16.253.191:80 check
        server staticsrv2 172.16.253.191:8080 check
[root@HAProxy ~]# systemctl restart haproxy   

配置haproxy主頁跳轉,即訪問的http的所有內容都跳轉到https的默認主頁上

[root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
    frontend https *:443
        bind *:443 ssl crt /etc/haproxy/certs/haproxy.pem
        acl static path_end .jpg .jpeg .png .gif .txt .html
        acl static path_beg /imgs /images /css
        use_backend staticsrvs  if static
        default_backend dynsrvs
    frontend http
        bind *:8080
        redirect location https://172.16.253.108/ if !{ ssl_fc }
    backend dynsrvs
        balance roundrobin
        server dynsrv1 172.16.253.105:80 check
        server dynsrv2 172.16.253.105:8080 check
    backend staticsrvs
        balance roundrobin
        server staticsrv1 172.16.253.191:80 check
        server staticsrv2 172.16.253.191:8080 check
[root@HAProxy ~]# systemctl restart haproxy   

client訪問測試


免責聲明!

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



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