haproxy 安装与反向代理简单配置

haproxy 下载

https://www.haproxy.org/#down

安装参考: https://blog.csdn.net/keil_wang/article/details/89712380

tar xzvf haproxy-1.8.13.tar.gz
cd haproxy-1.8.13/
make TARGET=linux31      //这里用uname -r查看系统内核版本。centos6.x使用linux26,centos7.x使用linux31
make install PREFIX=/usr/local/haproxy
mkdir /usr/local/haproxy/conf
cp examples/option-http_proxy.cfg /usr/local/haproxy/conf/haproxy.cfg



启动:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

配置文件  haproxy.cfg 配置反向代理

global
        maxconn         20000
	ulimit-n	16384
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
	nbproc		4
        daemon



#---------------------------------------------------------------------
# 默认配置
#---------------------------------------------------------------------
defaults
    mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    log global #采用全局定义的日志
    option httplog #日志类别,采用httplog
    option dontlognull #不记录健康检查日志信息
    retries 3 #3次连接失败就认为是服务器不可用,也可以通过后面设置
    option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
    #option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现,关闭keep-alive
    #option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持
    option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
    maxconn 6000 #默认的最大连接数
    timeout connect 50000ms #连接超时
    timeout client 300000ms #客户端超时
    timeout server 300000ms #服务器超时
    #timeout check 2000 #心跳检测超时
    #timeout http-keep-alive 10s #默认持久连接超时时间
    #timeout http-request 10s #默认http请求超时时间
    #timeout queue 1m #默认队列超时时间
    balance roundrobin #设置默认负载均衡方式,轮询方式,类似于nginx的ip_hash


    # 保持长连接
    option http-keep-alive
    option http-server-close
    timeout http-keep-alive 650s
    timeout client 30s





frontend test-proxy
	#bind		192.168.200.10:8080
       # 这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。
        bind *:80
        mode            http
        log             global
        option          httplog
        option          dontlognull
        option          nolinger
        option          http_proxy
        maxconn         8000
        timeout client  30s

	# layer3: Valid users
	#acl allow_host src 192.168.200.150/32
	#http-request deny if !allow_host

	# layer7: prevent private network relaying
	#acl forbidden_dst url_ip 192.168.0.0/24
	#acl forbidden_dst url_ip 172.16.0.0/12
	#acl forbidden_dst url_ip 10.0.0.0/8

        #配置第一个项目请求 ,比如将 http://localhost/t1/** 都代理  project1-server 服务
        #acl project1-server path_beg -i /t1/
        #use_backend project1-server if project1-server

       # 这种方式必须开启长连接         
        acl test  path_beg -i /test/
        use_backend test if test
       
        # 完全代理 ,即将  http://localhost/** 都 代理 test_all 的服务器
	#use_backend test_all
        

	#http-request deny if forbidden_dst
	default_backend test-proxy-srv


 #当以域名+/project1请求时转发到下面地址,内网ip+端口+项目名(项目名一个时可以不写)    
# check inter 10s 请不要随便加上这个配置,特别是 代理NGINX的时候,或者不支持心跳检查的服务,否则代理出错
backend project1-server
    mode http
    option forwardfor
    server wget 192.168.10.4:80/t1 check inter 10s




backend test
    mode http
    #option forwardfor
    server test1 192.168.10.4:8085 


backend test_all
    mode http
    #option forwardfor   # 让 代理的服务器能够识别代理的机器IP
    server n1  192.168.10.4:8085 



backend test-proxy-srv
	mode            http
	timeout connect 5s
	timeout server  5s
	retries         2
	option          nolinger
	option          http_proxy

	# layer7: Only GET method is valid
	acl valid_method        method GET
	http-request deny if !valid_method

	# layer7: protect bad reply
	http-response deny if { res.hdr(content-type) audio/mp3 }

 

参考

https://blog.csdn.net/keil_wang/article/details/89712380

 https://vegetable-chicken.blog.csdn.net/article/details/107717290

https://my.oschina.net/gongfuxiang/blog/4545141

https://my.oschina.net/u/4367553/blog/3546402
haproxy 保持连接: https://blog.51cto.com/fengwan/1775083

https://blog.51cto.com/tianshili/1841003

https://blog.51cto.com/fengwan/1775083


https://blog.csdn.net/boyce_avenue/article/details/80487161
https://blog.csdn.net/weixin_33984032/article/details/92179538

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章