Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7428797
  • 博文数量: 1758
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16252
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1758)

文章存档

2024年(3)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: 系统运维

2018-09-19 16:15:09

转:

  1. # Haproxy的常见配置
  2. # 参考文档:
  3. #
  4. # http://blog.sina.com.cn/s/blog_704836f40102w243.html
  5. #
  6. #
  7. # https://blog.codecentric.de/en/2014/12/haproxy-http-header-rate-limiting/
  8. #
  9. # https://www.haproxy.com/blog/websockets-load-balancing-with-haproxy/

  10. global
  11.   daemon
  12.   maxconn 20000
  13.   stats socket /var/run/haproxy/haproxy.sock mode 0600 level admin
  14.   log 127.0.0.1 local3

  15. defaults
  16.   mode http
  17.   
  18.   # 启动Haproxy web统计页面
  19.   stats uri /haproxy-admin
  20.   stats realm Haproxy\ Statistics
  21.   stats auth 用户名:密码

  22.   # 添加X-Forwarded-For头,使后端获得真实IP
  23.   option forwardfor except 127.0.0.1
  24.   
  25.   # 开启http keep-alive
  26.   option http-keep-alive
  27.   
  28.   # http keep-alive保持5秒
  29.   timeout http-keep-alive 5s
  30.   
  31.   # 开启日志
  32.   log 127.0.0.1 local3

  33. frontend http-in
  34.   
  35.   # 绑定80端口
  36.   bind *:80
  37.   
  38.   # 最大连接数
  39.   maxconn 20000
  40.   
  41.   # 客户端响应超时
  42.   timeout client 5s
  43.   # http请求送达超时
  44.   timeout http-request 5s
  45.   # http keep-alive保持连接最长时间
  46.   timeout http-keep-alive 5s
  47.   # half-open connection未响应超时,针对web socket
  48.   timeout client-fin 30s
  49.   
  50.   # 使用后端服务器组
  51.   default_backend servers
  52.   
  53.   # START 过速请求的防御
  54.   
  55.   # 创建stick-table,记录 cookie(SESSIONID) -> 最近30秒内http请求次数
  56.   # stick-table type string len 50 size 1m expire 10m store http_req_rate(30s)
  57.   
  58.   # 将cookie(SESSION)作为key,存到stick-table中
  59.   # http-request track-sc0 req.cook(SESSION)
  60.   
  61.   # 定义ACL,请求次数是否超过100
  62.   # acl abuse sc0_http_req_rate gt 100
  63.   
  64.   # 如果ACL为true,则拒绝http请求,响应429
  65.   # http-request deny deny_status 429 if abuse
  66.   
  67.   # END
  68.   
  69.   # START IP 黑名单
  70.   
  71.   # 定义ACL,看client ip是否在ip-blacklist.txt内,文件内容如下:
  72.   # xxx.xxx.xxx.xxx
  73.   # xxx.xxx.xxx.xxx/8
  74.   # acl block_ip src -f path/to/ip-blacklist.txt
  75.   
  76.   # 如果ACL为true,则拒绝http请求
  77.   # http-request deny if block_ip
  78.   
  79.   # END
  80.   
  81.   # START User-Agent黑名单
  82.   
  83.   # 定义ACL,看user-agent头是否字符串substring在ua-blacklist.txt内,文件内容如下:
  84.   # okhttp
  85.   # chrome
  86.   # acl block_ua hdr_sub(user-agent) -i -f path/to/ua-blacklist.txt
  87.   
  88.   # 如果ACL为true,则拒绝http请求
  89.   # http-request deny if block_ua
  90.   
  91.   # END
  92.   
  93. backend servers
  94.   # 连接到服务器超时
  95.   timeout connect 30s
  96.   # 服务器无活动超时
  97.   timeout server 60s
  98.   # tunnel超时,针对web socket
  99.   timeout tunnel 1h

  100.   # START 负载策略:轮询,session持久策略:cookie,这个策略最均匀
  101.   balance roundrobin
  102.   cookie SERVERNAME insert indirect nocache
  103.   # END
  104.   
  105.   # START 负载策略:轮询,session持久策略:ip
  106.   # balance roundrobin
  107.   # stick-table type ip size 10m expire 3h
  108.   # stick on src
  109.   # END
  110.   
  111.   # START 负载策略:IP hash
  112.   # balance source
  113.   # END
  114.   
  115.   # xxx代表服务地址(可带端口号)
  116.   # server1,2,3,4只是名字,可以改成需要的名字
  117.   # check port yyyy 代表通过检测后端服务器的yyyy端口来判断服务器是否可用,如果不可用,会自动切换
  118.   server server1 xxx check port yyyy maxconn 1000 maxqueue 10 slowstart 60s
  119.   server server2 xxx check port yyyy maxconn 1000 maxqueue 10 slowstart 60s
  120.   server server3 xxx check port yyyy maxconn 1000 maxqueue 10 slowstart 60s
  121.   server server4 xxx check port yyyy maxconn 1000 maxqueue 10 slowstart 60s



阅读(1590) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~