haproxy配置基於ssl證書的https負載均衡

本實驗所有在haproxy1.5.19版本進行測試經過,通過測試1.7.X及haproxy1.3版本如下haproxy配置參數可能不適用,須要注意版本號。php

1、業務要求
如今根據業務的實際須要,有如下幾種不一樣的需求。以下:html

1.1 http跳轉https前端

把全部請求http://www.chinasoft.com的地址所有跳轉爲https//:www.chinasoft.com這個地址node

1.2 http與https並存linux

服務器同時開放http://www.chinasoft.com和https://www.chinasoft.com的訪問形式web

1.3 服務器環境準備redis

node1即haproxy所在服務器的處理apache

安裝依賴
yum install -y openssl openssl-devel readline-devel pcre-devel libssl-dev libpcre3vim

# 下載安裝包,
tar zxf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19後端

useradd -u 188 -r -d /var/lib/haproxy -s /sbin/nologin haproxy
# 加入支持ssl的編譯參數

make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1
make install PREFIX=/usr/local/haproxy
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
cp examples/haproxy.init /etc/init.d/haproxy

  chmod +x /etc/init.d/haproxy

  mkdir /etc/haproxy

  mkdir /var/lib/haproxy

# 修改啓動腳本(可能會報錯)爲以下
vim /etc/init.d/haproxy
26 [[ ${NETWORKING} = "no" ]] && exit 0

後端web01(192.168.3.200)服務器apache配置,須要配置虛擬主機域名爲:www.chinasoft.com不然沒法正常處理

[root@node2 ~]# egrep -v '#|^$' /etc/httpd/conf/httpd.conf 
ServerRoot "/etc/httpd"
Listen 8080
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
Options FollowSymLinks
AllowOverride none
Allow from all
</Directory>
DocumentRoot "/var/www/html/chinasoft"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html/chinasoft">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf

[root@node2 ~]# cat /etc/httpd/conf.d/vhost.conf 
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot /var/www/html/
ServerName 192.168.3.200:8080
</VirtualHost>

<Directory "/var/www/html/chinasoft/">
php_admin_value open_basedir "/var/www/html/chinasoft/:/tmp/"
Options Includes ExecCGI FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:8080>
DocumentRoot /var/www/html/chinasoft/
ServerName www.chinasoft.com:8080
</VirtualHost>

 

1.4 證書的處理,須要將網站的根證書和key簡單的合併在一塊兒:

cat chinasoft.com.pem chinasoft.com.key | tee chinasoft.pem

 

不然會報錯
'bind *:443' : unable to load SSL private key from PEM file

1.5 域名的指向及處理
將www.chinasoft.com指向haproxy負載均衡器所在的服務器IP地址,此處是192.168.3.198

2、配置haproxy並測試業務需求
如今咱們根據業務的需求,咱們來配置haproxy一一達到其需求。

2.1 http跳轉https配置

http跳轉https的haproxy配置文件內容,以下:

[root@node1 haproxy]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local3 info
chroot /var/lib/haproxy
maxconn 4096
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
stats timeout 2m
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

listen admin_stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s
stats uri /haproxy?stats
stats auth admin:admin
stats hide-version

frontend weblb
bind *:80
acl is_http hdr_beg(host) www.chinasoft.com
redirect scheme https if !{ ssl_fc }
bind *:443 ssl crt /etc/haproxy/chinasoft.pem
use_backend httpserver if is_http

backend httpserver
balance source
server web1 192.168.3.200:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

 

# 配置好以後先檢查語法是否正確
[root@node1 haproxy]# /etc/init.d/haproxy check
Configuration file is valid

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

tune.ssl.default-dh-param 2048由於咱們的SSL密鑰使用的是2048bit加密,因此在此進行聲明。
acl is_http hdr_beg(host) www.chinasoft.com
redirect scheme https if !{ ssl_fc }
bind *:443 ssl crt /etc/haproxy/chinasoft.pem

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

管理頁面

2.2 測試http跳轉https

http跳轉https配置完畢後,咱們選擇來測試其跳轉。以下:

你會發如今瀏覽器中,不管你輸入的是www.chinasoft.com,仍是http://www.chinasoft.com亦或是https://www.chinasoft.com,都會自動跳轉到https://www.chinasoft.com。

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


2.3 http與https並存配置
haproxy要實現http和https並存的話,配置也很簡單,只須要把haproxy分別監控不一樣的端口就行,配置文件以下:

[root@node1 haproxy]# cat haproxy.cfg
global
log 127.0.0.1 local3 info
chroot /var/lib/haproxy
maxconn 4096
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
stats timeout 2m
tune.ssl.default-dh-param 2048

defaults
log global
mode http
option httplog
option dontlognull
option redispatch
retries 3
option redispatch
maxconn 2000
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s

listen admin_stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s
stats uri /haproxy?stats
stats auth admin:admin
stats hide-version

frontend weblb
bind *:80
acl is_http hdr_beg(host) www.chinasoft.com
use_backend httpserver if is_http

backend httpserver
balance source
server web1 192.168.3.200:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

frontend weblb443
bind *:443 ssl crt /etc/haproxy/chinasoft.pem
acl is_443 hdr_beg(host) www.chinasoft.com
use_backend httpserver443 if is_443

backend httpserver443
balance source
server web1 192.168.3.200:8080 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並存配置完畢後,咱們選擇來測試其跳轉。以下:

 

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

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

 

生產環境配置實例:

[root@u05mix05 ~]# cat /etc/haproxy/haproxy.cfg 
global
log 127.0.0.1 local3 info
chroot /var/lib/haproxy
maxconn 20480
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
stats timeout 2m

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 20000
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:6080
    mode http
    option httplog
    maxconn 10
    stats refresh 30s
    stats uri /haproxy?stats
    stats auth admin:admin
    stats hide-version

frontend hs_chinasoft_com
    mode http
    bind *:9735
    stats uri /haproxy?stats
    default_backend hs_chinasoft_com_backend

backend hs_chinasoft_com_backend
    option forwardfor header X-REAL-IP
    option httpchk GET /check
    balance roundrobin
    server node1 1.1.1.1:9735 check inter 10000 rise 3 fall 3 weight 1

frontend hs_chinasoft_info
        mode http
        bind *:9800
        stats uri /haproxy?stats
        default_backend hs_chinasoft_info_backend

backend hs_chinasoft_info_backend
        option forwardfor header X-REAL-IP
        option httpchk GET /check
        balance roundrobin
        server node1 1.1.1.1:9800 check inter 15000 rise 3 fall 3 weight 1
        server node2 1.1.1.2:9800 check inter 15000 rise 3 fall 3 weight 1
相關文章
相關標籤/搜索