操作背景:
前方有一台haproxy代理機器(115.100.120.57/192.168.1.7),后方兩台realserver機器(192.168.1.150、192.168.1.151,沒有公網ip,部署了很多站點)
將域名解析到haproxy機器的公網ip,在haproxy配置文件里,根據域名轉發至后端realserver上。
haproxy代理配置:根據域名進行轉發(即后端機器不管部署多少個域名,都可以直接在haproxy配置文件里通過域名對域名方式直接指定)
nginx代理配置:根據端口進行轉發(即后端機器部署多個域名時,在前面nginx配置里通過upstream負載到不同的端口上)
----------------------------------------------------------------------------------------------------------
下面是haproxy代理服務器上的操作
(1)關閉SElinux、配置防火牆 (后端realserver機器的iptables防火牆可以限制只允許代理服務器訪問)
[root@localhost ~]# vim /etc/config/selinux
SELINUX=disabled
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
[root@localhost ~]# vim /etc/sysconfig/iptables
.......
-A INPUT -s 115.100.120.0/24 -d 224.0.0.18 -j ACCEPT #允許組播地址通信(這里,我將機器的公網和私網地址都放了)
-A INPUT -s 192.168.1.0/24 -d 224.0.0.18 -j ACCEPT
-A INPUT -s 115.100.120.0/24 -p vrrp -j ACCEPT #允許 VRRP(虛擬路由器冗余協)通信
-A INPUT -s 192.168.1.0/24 -p vrrp -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
[root@localhost ~]# /etc/init.d/iptables restart
(2)安裝haproxy
1)安裝編譯工具
[root@localhost ~]# yum install gcc gcc-c++ make openssl-devel kernel-devel
2)安裝 haproxy
下載 haproxy,放到/usr/local/src目錄下
haproxy-1.5.14.tar.gz下載:https://pan.baidu.com/s/1jI8qiyy
提取密碼:u77d
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# ls haproxy-1.5.14.tar.gz
[root@localhost ~]# tar zxvf haproxy-1.5.14.tar.gz
[root@localhost ~]# cd haproxy-1.5.14
接着進行haproxy的編譯安裝,haproxy編譯沒有configure,直接make->make install
[root@localhost haproxy-1.5.14]# make TARGET=linux26 CPU=x86_64 PREFIX=/usr/local/haprpxy USE_OPENSSL=1 ADDLIB=-lz
[root@localhost haproxy-1.5.14]# ldd haproxy | grep ssl
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007f06ae977000)
[root@localhost haproxy-1.5.14]# make install PREFIX=/usr/local/haproxy
編譯參數說明:
TARGET=linux26 : 使用 uname -r 查看內核,如:2.6.18-371.el5,此時該參數就為linux26
CPU=x86_64 : 使用 uname -r 查看系統信息,如 x86_64 x86_64 x86_64 GNU/Linux,此時該參數就為 x86_64
PREFIX : 后面跟的是haprpxy的安裝路徑
USE_OPENSSL=1 ADDLIB=-lz : 支持ssl
3)設置 haproxy
[root@localhost src]# mkdir -p /usr/local/haproxy/conf
[root@localhost src]# mkdir -p /etc/haproxy
[root@localhost src]# cp /usr/local/src/haproxy-1.5.14/examples/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg
[root@localhost src]# ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg
[root@localhost src]# cp -r /usr/local/src/haproxy-1.5.14/examples/errorfiles /usr/local/haproxy/errorfiles
[root@localhost src]# ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles
[root@localhost src]# mkdir -p /usr/local/haproxy/log
[root@localhost src]# touch /usr/local/haproxy/log/haproxy.log
[root@localhost src]# ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log
[root@localhost src]# cp /usr/local/src/haproxy-1.5.14/examples/haproxy.init /etc/rc.d/init.d/haproxy
[root@localhost src]# chmod +x /etc/rc.d/init.d/haproxy
[root@localhost src]# chkconfig haproxy on
[root@localhost src]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin
4)配置 haproxy.cfg 參數
[root@localhost src]# cp /usr/local/haproxy/conf/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg-bak
[root@localhost src]# vim /usr/local/haproxy/conf/haproxy.cfg #可以直接粘貼進去使用
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0 info #在本機記錄日志
maxconn 65535 #每個進程可用的最大連接數
chroot /usr/local/haproxy #haproxy 安裝目錄
uid nobody #運行haproxy的用戶uid(使用id號也行)
gid nobody #運行haproxy的組uid(使用id號也行)
daemon #以后台守護進程運行
defaults
log global
mode http #運行模式tcp、http、health
retries 3 #三次連接失敗,則判斷服務不可用
option redispatch #如果后端有服務器宕機,強制切換到正常服務器
stats uri /haproxy #統計頁面 URL 路徑
stats refresh 30s #統計頁面自動刷新時間
stats realm haproxy-status #統計頁面輸入密碼框提示信息
stats auth admin:dxInCtFianKtL]36 #統計頁面用戶名和密碼
stats hide-version #隱藏統計頁面上 HAProxy 版本信息
maxconn 65535 #每個進程可用的最大連接數
timeout connect 5000 #連接超時
timeout client 50000 #客戶端超時
timeout server 50000 #服務端超時
frontend http-in #自定義描述信息
mode http #運行模式tcp、http、health
maxconn 65535 #每個進程可用的最大連接數
bind :80 #監聽80端口
log global
option httplog
option httpclose #每次請求完畢后主動關閉 http 通道
acl is_1 hdr_beg(host) -i testwww.huanqiu.com #規則設置,-i后面是要訪問的域名;多個域名,就寫多個規則,但is_1/2/...要與后面的use_backend 對應
acl is_2 hdr_beg(host) -i testchina.huanqiu.com
acl is_3 hdr_beg(host) -i testworld.huanqiu.com
acl is_4 hdr_beg(host) -i testgame.huanqiu.com
acl is_5 hdr_beg(host) -i testtech.huanqiu.com
acl is_6 hdr_beg(host) -i betawww.huanqiu.com
acl is_7 hdr_beg(host) -i betachina.huanqiu.com
acl is_8 hdr_beg(host) -i betaworld.huanqiu.com
acl is_9 hdr_beg(host) -i betagame.huanqiu.com
acl is_10 hdr_beg(host) -i betatech.huanqiu.com
use_backend test-server if is_1 #如果訪問is_1設置的域名,就負載均衡到下面backend設置的對應test-server上,其他的域名同理
use_backend test-server if is_2
use_backend test-server if is_3
use_backend test-server if is_4
use_backend test-server if is_5
use_backend beta-server if is_6 #如果訪問is_6設置的域名,就負載均衡到下面backend設置的對應beta-server上,其他的域名同理
use_backend beta-server if is_7
use_backend beta-server if is_8
use_backend beta-server if is_9
use_backend beta-server if is_10
backend test-server
mode http
balance roundrobin #設置負載均衡模式,source 保存 session 值,roundrobin 輪詢模式
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 192.168.1.150:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5 #由於后端同組的機器只有一台,這里就設置一台;如果后端是多台的話,就都加上,進行負載。
backend beta-server
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 192.168.1.151:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
---------------------------------------------------------
參數解釋
#inter 2000 心跳檢測時間;
rise 2 三次連接成功,表示服務器正常;
fall 5 三次連接失敗,表示服務器異常;
weight 1 權重設置
---------------------------------------------------------
[root@localhost ~]#service haproxy start #啟動
[root@localhost ~]#service haproxy stop #關閉
[root@localhost ~]#service haproxy restart #重啟
[root@localhost ~]#service haproxy restart #查看狀態
5)啟動haproxy日志
haproxy日志配置好之后,如上haproxy日志路徑是/var/log/haproxy.log,但是默認情況下,haproxy日志是沒有記錄的!
需要做如下設定:
[root@localhost ~]# vim /etc/rsyslog.conf
.............
$ModLoad imudp #去掉這行注釋
$UDPServerRun 514 #去掉這會注釋,rsyslog 默認情況下,需要在514端口監聽UDP
local0.* /var/log/haproxy.log #添加這一行,這個日志文件是在上面設置的
重啟rsyslog服務
[root@localhost ~]# /etc/init.d/rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
訪問網站,查看haproxy日志:
[root@kvm-server conf]# tail -f /var/log/haproxy.log
tail: inotify cannot be used, reverting to polling
Sep 21 15:17:22 localhost haproxy[85360]: 124.65.197.154:2546 [21/Sep/2016:15:17:22.719] http-in test-server/web01 0/0/1/130/131 200 2692 - - --VN 4/4/0/0/0 0/0 "GET / HTTP/1.1"
Sep 21 15:17:22 localhost haproxy[85360]: 124.65.197.154:2546 [21/Sep/2016:15:17:22.719] http-in test-server/web01 0/0/1/130/131 200 2692 - - --VN 4/4/0/0/0 0/0 "GET / HTTP/1.1"
Sep 21 15:17:24 localhost haproxy[85360]: 124.65.197.154:11482 [21/Sep/2016:15:17:22.719] http-in test-server/web01 1754/0/1/44/1799 404 615 - - --VN 3/3/0/0/0 0/0 "GET /upload/20160325/201603251450171458888617180-100.jpg HTTP/1.1"
Sep 21 15:17:24 localhost haproxy[85360]: 124.65.197.154:11482 [21/Sep/2016:15:17:22.719] http-in test-server/web01 1754/0/1/44/1799 404 615 - - --VN 3/3/0/0/0 0/0 "GET /upload/20160325/201603251450171458888617180-100.jpg HTTP/1.1"
Sep 21 15:17:40 localhost haproxy[85360]: 124.65.197.154:13161 [21/Sep/2016:15:17:22.719] http-in http-in/<NOSRV> -1/-1/-1/-1/17980 400 187 - - CR-- 2/2/0/0/0 0/0 "<BADREQ>"
Sep 21 15:17:40 localhost haproxy[85360]: 124.65.197.154:13161 [21/Sep/2016:15:17:22.719] http-in http-in/<NOSRV> -1/-1/-1/-1/17980 400 187 - - CR-- 2/2/0/0/0 0/0 "<BADREQ>"
6)查看統計頁面的情況
可以根據haproxy.cfg里面配置的url進行訪問(用戶名和密碼也在配置文件里)
比如:
http://testwww.huanqiu.com/haproxy


到此,haproxy的反向代理配置就已經結束了。
只有后端realserver的nginx里對於域名配置ok,就可以直接訪問域名,通過haproxy代理轉發到后端realserver上了~~
*******************************************************************************************************************
上面配置的haproxy是針對http方式的代理
下面接着說下haproxy針對https方式的代理
由於前面提到haproxy在編譯安裝的時候,已經支持ssl了
現在就需要添加pem證書即可!
由於在其他服務器上已經有了在用的證書,購買的*通配符的證書,所以直接拿過來用即可
證書存放路徑例如是/usr/local/haproxy/ssl
[root@localhost ~]# mkdir /usr/local/haproxy/ssl
[root@localhost ~]# cd /usr/local/haproxy/ssl
[root@localhost ssl]# ls
huanqiu.cer huanqiu.key
制作成pem證書
[root@localhost ssl]# cat huanqiu.cer huanqiu.key | tee huanqiu.pem
[root@localhost ssl]# ls
huanqiu.cer huanqiu.key huanqiu.pem
----------------------------------------------------------------------------------------------------
如果沒有提前准備好的證書,就需要手動創建,創建方法如下:
[root@localhost ssl]# openssl genrsa -des3 -out huanqiu.key 1024
Generating RSA private key, 1024 bit long modulus
................................++++++
....................................++++++
e is 65537 (0x10001)
Enter pass phrase for huanqiu.key: #提示輸入密碼,比如這里我輸入123456
Verifying - Enter pass phrase for huanqiu.key: #確認密碼,繼續輸入123456
[root@localhost ssl]# ls #查看,已生成CSR(Certificate Signing Request)文件
huanqiu.key
[root@localhost ssl]# openssl req -new -key huanqiu.key -out huanqiu.csr
Enter pass phrase for huanqiu.key: #輸入123456
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn #國家
State or Province Name (full name) []:beijing #省份
Locality Name (eg, city) [Default City]:beijing #地區名字
Organization Name (eg, company) [Default Company Ltd]:huanqiu #公司名
Organizational Unit Name (eg, section) []:Technology #部門
Common Name (eg, your name or your server's hostname) []:huanqiu #CA主機名
Email Address []:wangshibo@huanqiu.com #郵箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456 #證書請求密鑰,CA讀取證書的時候需要輸入密碼
An optional company name []:huanqiu #-公司名稱,CA讀取證書的時候需要輸入名稱
[root@localhost ssl]# ls
huanqiu.csr huanqiu.key
[root@localhost ssl]# cp huanqiu.key huanqiu.key.bak
[root@localhost ssl]# openssl rsa -in huanqiu.key.bak -out huanqiu.key
Enter pass phrase for huanqiu.key.bak: #輸入123456
writing RSA key
[root@linux-node1 ssl]# openssl x509 -req -days 365 -in huanqiu.csr -signkey huanqiu.key -out huanqiu.crt
Signature ok
subject=/C=cn/ST=beijing/L=beijing/O=huanqiu/OU=Technology/CN=huanqiu/emailAddress=wangshibo@huanqiu.com
Getting Private key
[root@linux-node1 ssl]# ll
total 24
-rw-r--r-- 1 root root 960 Sep 12 16:01 huanqiu.crt
-rw-r--r-- 1 root root 769 Sep 12 15:59 huanqiu.csr
-rw-r--r-- 1 root root 887 Sep 12 16:01 huanqiu.key
-rw-r--r-- 1 root root 963 Sep 12 16:01 huanqiu.key.bak
----------------------------------------------------------------------------------------------------
接着在haproxy.cfg里添加ssl代理的配置內容
比如:配置https://testwww.xqshijie.com和https://betawww.xqshijie.com的代理內容,分別轉發到192.168.1.150和192.168.1.151上(如果是多個域名的https方式代理,同理,直接在下面對應區域添加配置即可)
[root@linux-node1 conf]# pwd
/usr/local/haproxy/conf
[root@linux-node1 conf]# cat haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0 info
maxconn 65535
chroot /usr/local/haproxy
uid nobody
gid nobody
daemon
defaults
log global
mode http
retries 3
option redispatch
stats uri /haproxy
stats refresh 30s
stats realm haproxy-status
stats auth admin:dxInCtFianKtL]36
stats hide-version
maxconn 65535
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
mode http
maxconn 65535
bind :80
log global
option httplog
option httpclose
acl is_1 hdr_beg(host) -i testwww.huanqiu.com
acl is_2 hdr_beg(host) -i testchina.huanqiu.com
acl is_3 hdr_beg(host) -i testworld.huanqiu.com
acl is_4 hdr_beg(host) -i testgame.huanqiu.com
acl is_5 hdr_beg(host) -i testtech.huanqiu.com
acl is_6 hdr_beg(host) -i betawww.huanqiu.com
acl is_7 hdr_beg(host) -i betachina.huanqiu.com
acl is_8 hdr_beg(host) -i betaworld.huanqiu.com
acl is_9 hdr_beg(host) -i betagame.huanqiu.com
acl is_10 hdr_beg(host) -i betatech.huanqiu.com
use_backend test-server if is_1
use_backend test-server if is_2
use_backend test-server if is_3
use_backend test-server if is_4
use_backend test-server if is_5
use_backend beta-server if is_6
use_backend beta-server if is_7
use_backend beta-server if is_8
use_backend beta-server if is_9
use_backend beta-server if is_10
use_backend test-server if is_11
listen TEST_APP_SSL
bind *:443 ssl crt /usr/local/haproxy/ssl/xqshijie.pem
reqadd X-Forwarded-Proto:\ https
mode http
acl is_a hdr_beg(host) -i testwww.huanqiu.com
acl is_b hdr_beg(host) -i betawww.huanqiu.com
use_backend test-server if is_a
use_backend beta-server if is_b
backend test-server
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 192.168.1.150:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
backend beta-server
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 192.168.1.151:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
重啟haproxy服務
[root@linux-node1 conf]# service haproxy restart
這樣,訪問https://testwww.huanqiu.com和https://betawww.huanqiu.com的請求就會被haproxy代理轉發到
后端192.168.1.150和192.168.1.151服務器上對應域名的443端口!
到此,haproxy的http和https方式的代理部署均已完成!
----------------------------------------------------------------------------------------------------------------------------------
下面說下haproxy代理非http/https方式的配置,比如代理mysql、ftp等其他端口~
當代理非80/443端口是,mode模式就是tcp了
所以要注釋掉或去掉上面配置在haproxy.cnf里defaults局域的mode https的默認配置
[root@linux-node1 conf]# cat haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0 info
maxconn 65535
chroot /usr/local/haproxy
uid nobody
gid nobody
daemon
defaults
log global
# mode http
retries 3
option redispatch
stats uri /haproxy
stats refresh 30s
stats realm haproxy-status
stats auth admin:dxInCtFianKtL]36
stats hide-version
maxconn 65535
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
mode http
maxconn 65535
bind :80
log global
option httplog
option httpclose
acl is_1 hdr_beg(host) -i testwww.huanqiu.com
acl is_2 hdr_beg(host) -i testchina.huanqiu.com
acl is_3 hdr_beg(host) -i testworld.huanqiu.com
acl is_4 hdr_beg(host) -i testgame.huanqiu.com
acl is_5 hdr_beg(host) -i testtech.huanqiu.com
acl is_6 hdr_beg(host) -i betawww.huanqiu.com
acl is_7 hdr_beg(host) -i betachina.huanqiu.com
acl is_8 hdr_beg(host) -i betaworld.huanqiu.com
acl is_9 hdr_beg(host) -i betagame.huanqiu.com
acl is_10 hdr_beg(host) -i betatech.huanqiu.com
use_backend test-server if is_1
use_backend test-server if is_2
use_backend test-server if is_3
use_backend test-server if is_4
use_backend test-server if is_5
use_backend beta-server if is_6
use_backend beta-server if is_7
use_backend beta-server if is_8
use_backend beta-server if is_9
use_backend beta-server if is_10
use_backend test-server if is_11
listen TEST_APP_SSL
bind *:443 ssl crt /usr/local/haproxy/ssl/xqshijie.pem
reqadd X-Forwarded-Proto:\ https
mode http
acl is_a hdr_beg(host) -i testwww.huanqiu.com
acl is_b hdr_beg(host) -i betawww.huanqiu.com
use_backend test-server if is_a
use_backend beta-server if is_b
backend test-server
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 192.168.1.150:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
backend beta-server
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 192.168.1.151:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
listen mysql_73 0.0.0.0:33061 #代理mysql端口
mode tcp
option tcplog
balance roundrobin
option tcpka
option httpchk
option mysql-check user haproxy
server 10.68.250.73 10.68.250.73:3306 weight 1 check inter 1s rise 2 fall 2
listen mysql_164 0.0.0.0:33062
mode tcp
option tcplog
balance roundrobin
option tcpka
option httpchk
option mysql-check user haproxy
server 10.68.250.164 10.68.250.164:3306 weight 1 check inter 1s rise 2 fall 2
frontend ftp_service #代理ftp(被動模式,端口范圍:40000-40003)
mode tcp
bind *:2021
default_backend ftp_server
frontend ftp
mode tcp
bind *:40000-40003
default_backend ftp_server_data
backend ftp_server
mode tcp
server ftp 10.68.250.198 check port 2021 inter 10s rise 1 fall 2
backend ftp_server_data
mode tcp
server ftp 10.68.250.198 check port 2021 inter 10s rise 1 fall 2
listen node1-9200 0.0.0.0:19200 #本機19200端口代理192.168.1.160的9200端口
mode tcp
option tcplog
balance roundrobin
server 192.168.1.160 192.168.1.160:9200 weight 1 check inter 1s rise 2 fall 2
listen node2-9200 0.0.0.0:19201 #本機19200端口代理192.168.1.160的9200端口
mode tcp
option tcplog
balance roundrobin
server 192.168.1.160 192.168.1.161:9200 weight 1 check inter 1s rise 2 fall 2
重啟haproxy(重啟時的WARNING不影響)
[root@kvm-server conf]# /etc/init.d/haproxy restart
對以上的開放端口要在防火牆iptables里開通
[root@linux-node1 conf]# cat /etc/sysconfig/iptables
.............
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 33061 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 33062 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2021 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 40000:40003 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 19200 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 19201 -j ACCEPT
以上就是haproxy代理mysql、ftp及其他端口的配置~~~
----------------------------------------------------------------------------------
如下設置,通過haproxy將本機8080和80端口分別反向代理到192.168.34.37的8080以及192.168.72.242的80端口。
[root@ha01 conf]# cat haproxy.cfg
#
# demo config for Proxy mode
#
global
maxconn 20000
ulimit-n 65535
log 127.0.0.1 local0
uid 99
gid 99
chroot /var/empty
nbproc 4
daemon
listen wiki
bind :8080
mode tcp
timeout connect 10000ms
timeout client 5000ms
timeout server 5000ms
server t1 192.168.34.37:8080
listen caf
bind :81
mode http
timeout connect 10000ms
timeout client 5000ms
timeout server 5000ms
server t1 192.168.72.242:81
