haproxy详解
HAProxy
使用C語言編寫的開放源代碼軟件,提供高可用性、負(fù)載均衡,以及基于TCP(4層)和HTTP(7層)的應(yīng)用程序代理;
借助HAProxy可以快速并可靠的提供基于TCP和HTTP應(yīng)用的代理解決方案,并支持虛擬主機;
HAProxy實現(xiàn)了一種時間驅(qū)動,單一進程模型,此模型支持非常大的鏈接數(shù);
HAProxy支持拒絕連接:維護連接開銷低,可以限制***蠕蟲(attack bots),該功能拯救了很多被DDos***的小型站點;
HAProxy支持全透明代理(具備防火墻的典型特點):可以用客戶端IP地址或者任何其他地址來連接后端服務(wù)器. 這個特性僅在Linux 2.4/2.6內(nèi)核打了cttproxy補丁后才可以使用. 這個特性也使得為某特殊服務(wù)器處理部分流量同時又不修改服務(wù)器的地址成為可能。
HAProxy的主要功能
1 - route HTTP requests depending on statically assigned cookies
2 - spread load among several servers while assuring server persistence
3 - through the use of HTTP cookies
4 - switch to backup servers in the event a main server fails
5 - accept connections to special ports dedicated to service monitoring
6 - stop accepting connections without breaking existing ones
7 - add, modify, and delete HTTP headers in both directions
8 - block requests matching particular patterns
9 - report detailed status to authenticated users from a URI intercepted by the application
程序環(huán)境
主程序:/usr/sbin/haproxy?
配置文件: /etc/haproxy/haproxy.cfg?
Unit file: /usr/lib/systemd/system/haproxy.service
配置段
global:全局配置段\
進程及安全配置相關(guān)的參數(shù)
性能調(diào)整相關(guān)參數(shù)
Debug參數(shù)
proxies:代理配置段
defaults:為frontend, listen, backend提供默認(rèn)配置;
frontend:前端,相當(dāng)于nginx, server {}
backend:后端,相當(dāng)于nginx, upstream {}
listen:同時擁前端和后端
global配置詳解
全局配置段
1 Process management and security
設(shè)置log,需開啟本機的rsyslog服務(wù),可編輯/etc/rsyslog.conf進行定義,配置以下兩行
$InputTCPServerRun?514local2.*????????????????????????????????????????????????/var/log/haproxy.logPerformance tuning
系統(tǒng)調(diào)整
Debugging
Proxies配置詳解
代理配置段
Proxy configuration can be located in a set of sections :
defaults <name>
frontend <name>
backend <name>
listen <name>
bind
綁定ip及端口
bind [<address>]:<port_range> [, ...] [param*]
bind /<path> [, ...] [param*]
Define one or several listening addresses and/or ports in a frontend.
balance
定義調(diào)度類型
balance <algorithm> [ <arguments> ]
balance url_param <param> [check_post]
Define the load balancing algorithm to be used in a backend.
1 roundrobin:輪詢;
2 static-rr:根據(jù)權(quán)重輪流使用,但是靜態(tài)的,對服務(wù)器數(shù)量無限制,不支持平滑切換;
3 leastconn:連接數(shù)量最少的服務(wù)器優(yōu)先,當(dāng)連接數(shù)相同時使用roundrobin,不適用http;
4 frist:將第一臺連接滿,開始連接第二臺,第二臺連接滿連接第三臺;
5 source:基于源IP的綁定,相當(dāng)于Nginx的hash_ip,相當(dāng)于LVS的Source ip hashing;
6 URI:對URI進行hash,將同一個URI發(fā)送至同一個服務(wù)器,適用于緩存服務(wù)器,hash方式取決于hash_type;
7 url_param:根據(jù)指定參數(shù)進行調(diào)度,對參數(shù)進行hash,將統(tǒng)一用戶的信息都發(fā)送至同一臺服務(wù)器,hash方式取決于hash_type;
8 hdr(name):在每個HTTP請求中查找HTTP頭<name>,然后根據(jù)hash HTTP頭的name來進行調(diào)度,hash方式取決于hash_type;
9 rdp-cookie(name):查詢每個進來的TCP請求并hash,該機制用于退化的持久連接,hash方式取決于hash_type;
hash-type:哈希算法
hash-type <method> <function> <modifier>
Specify a method to use for mapping hashes to servers
default_backend
default_backend <backend>\
Specify the backend to use when no "use_backend" rule has been matched.
May?be?used?in?sections?: defaults frontend listen backend??yes?????????yes?????????yes?????no例 use_backend?????dynamic??if??url_dyn use_backend?????static???if??url_css?url_img?extension_img default_backend?dynamicserver
server <name> <address>[:[port]] [param*]
Declare a server in a backend 定義后端主機及其各種選項
May?be?used?in?sections?: defaults frontend listen backend????no??????????no??????????yes?????yes轉(zhuǎn)載于:https://blog.51cto.com/guoruilin198/1905687
總結(jié)
- 上一篇: CSS3几种新的长度单位
- 下一篇: DALI解码