haproxy acl 规则

简介:

haproxy acl 规则

1 按请求的主机头(名)负载

cat haproxy.cfg 
 
 
  1. global 
  2.     log 127.0.0.1 local1 
  3.     maxconn 65000             #最大连接数 
  4.     chroot /usr/local/haproxy #安装目录 
  5.     uid 99                    #用户haproxy 
  6.     gid 99                    #组haproxy 
  7.     daemon                    #守护进程运行 
  8.     nbproc 1                  #进程数量 
  9.     pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid 
  10.  
  11. defaults 
  12.    log     global 
  13.    mode    http               #7层 http;4层tcp  
  14.    option  httplog            #http 日志格式 
  15.    option  httpclose          #主动关闭http通道 
  16.    option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器 
  17.    option  forwardfor 
  18.    option  dontlognull 
  19.    maxconn 50000              #最大连接数 
  20.    contimeout      5000       #连接超时(毫秒) 
  21.    clitimeout      50000      #客户端超时(毫秒) 
  22.    srvtimeout      50000      #服务器超时(毫秒) 
  23.  
  24.    #errorfile 502 /usr/local/haproxy/html/maintain.html 
  25.    #errorfile 503 /usr/local/haproxy/html/maintain.html 
  26.    #errorfile 504 /usr/local/haproxy/html/maintain.html 
  27.   
  28.  
  29. frontend test.com             #定义前端服务器(haproxy) 
  30.         bind *:80             #监听地址 
  31.         acl web-client path_beg -i /vsphere-client 
  32.         acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum) 
  33.         acl monitor hdr_beg(host) -i monitor.test.com    #定义ACL名称,对应的请求的主机头是monitor.test.com  
  34.         acl www hdr_beg(host) -i www.test.com 
  35.         use_backend  cache.test.com if static    
  36.         use_backend  monitor.test.com if bbs or monitor 
  37.         use_backend  www.test.com if www 
  38.         use_backend  vsphere-client if web-client 
  39.  
  40.         default_backend www.test.com  #指定默认的后端服务器 
  41.  
  42.  
  43. backend monitor.test.com              #定义后端服务器群(web server/apache/nginx/iis..) 
  44.         mode http 
  45.         option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP 
  46.         balance leastconn     #负载均衡的方式,最小连接 
  47.         cookie SERVERID       #插入serverid到cookie中,serverid后面可以定义 
  48.         option  httpchk HEAD /check.html #用来做健康检查html文档 
  49.         #option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host 
  50.         server server1 10.0.100.70:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3 
  51.         #服务器定义: 
  52.         #cookie server1表示serverid为server1; 
  53.         #check inter 2000 是检测心跳频率(check 默认 ); 
  54.         #rise 3 表示 3次正确认为服务器可用; 
  55.         #fall 3 表示 3次失败认为服务器不可用; 
  56.         #weight 表示权重。 
  57.  
  58. backend www.test.com 
  59.         mode http 
  60.         option  forwardfor 
  61.         balance roundrobin    #负载均衡的方式,轮询方式 
  62.         cookie SERVERID   
  63.         option  httpchk HEAD /check.html  
  64.         server server1 10.0.100.71:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3 
  65.  
  66. backend vsphere-client 
  67.         mode http 
  68.         option  forwardfor header ORIG_CLIENT_IP 
  69.         balance roundrobin 
  70.         server server1 10.0.100.81:80 redir https://192.168.57.81:443 check inter 2000 rise 3 fall 3 weight 3 
  71.  
  72. backend cache.test.com 
  73.         option  forwardfor 
  74.         #balance uri len 15 #url hash 
  75.         balance roundrobin 
  76.         server server1 10.0.100.73:80  check inter 2000 rise 3 fall 3 weight 3 
  77.         server server2 10.0.100.75:80  check inter 2000 rise 3 fall 3 weight 3 
  78.  
  79. listen admin_stat                   #status 
  80.     bind 0.0.0.0:8080               #监听端口 
  81.     mode http                       #http的7层模式 
  82.     stats refresh 30s               #统计页面自动刷新时间 
  83.     stats uri /haproxy_stats_url    #统计页面URL 
  84.     stats realm Haproxy\ Statistics #统计页面密码框上提示文本 
  85.     stats auth admin:admin          #统计页面用户名和密码设置 
  86.     stats hide-version              #隐藏统计页面上HAProxy的版本信息 
  87.     stats admin if TRUE             #手工启用/禁用,后端服务器 
 
 
2 其它acl 规则
 
 
  1. ###########acl 开始了############ 
  2. acl bbs       hdr_reg(host) -i ^(bbs.test.com|forum.test.com)  #使用正则匹配 
  3. acl bbs_path  path_beg -i /bbs                #url 目录 
  4. acl youxi     path_beg -i /youxi               
  5. acl static    path_end -i .html .css .js      #url 结尾文件 
  6. acl php       path_end -i .php  
  7. acl jsp       path_end -i .jsp .do  
  8.  
  9. use_backend bbs_pool if bbs or bbs_path       #注意 "or"  
  10. use_backend youxi_pool if youxi 
  11. use_backend static_pool if static  
  12. use_backend php_pool if php 
  13. use_backend jsp_pool if jsp 
  14. default_backend www.test.com                 
  15. ###########acl 结束了############ 
 
 
#acl 参数
acl(关键字) 定义acl(名称)  方法           -i (忽略大小写)  [匹配的路径或文件]
                           hdr_beg(host)
                          hdr_reg(host)
                          path_beg
                          path_end
 
 
3 use_backend 参数
 
 
  1. or 用于匹配多个acl 名称 
  2. default_backend 没有满足条件的时候使用默认的后端服务器 
 
 
参考1 
 
参考2
http://xok.la/2010/07/haproxy.html  (404 了囧)
 
haproxy 重定向url (301)
 
  1. acl web-client path_beg -i /vsphere-client 
  2.  
  3. use_backend  vsphere-client if web-client 
  4.  
  5. backend vsphere-client 
  6.         mode http 
  7.         option  forwardfor header ORIG_CLIENT_IP 
  8.         balance roundrobin 
  9.         option  httpchk HEAD /check.html 
  10.         server server1 10.0.100.81:80 redir https://192.168.57.81:443 check inter 2000 rise 3 fall 3 weight 3  
测试192.168.57.82 为 haproxy ,192.168.57.81 为 https server
 
  1. curl -ILv http://192.168.57.82/vsphere-client 
  2. * About to connect() to 192.168.57.82 port 80 (#0) 
  3. *   Trying 192.168.57.82... connected 
  4. * Connected to 192.168.57.82 (192.168.57.82) port 80 (#0) 
  5. > HEAD /vsphere-client HTTP/1.1 
  6. > User-Agent: curl/7.21.6 (x86_64-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 librtmp/2.3 
  7. > Host: 192.168.57.82 
  8. > Accept: */* 
  9. >  
  10. < HTTP/1.1 302 Found 
  11. HTTP/1.1 302 Found 
  12. < Cache-Control: no-cache 
  13. Cache-Control: no-cache 
  14. < Content-length: 0 
  15. Content-length: 0 
  16. < Location: https://192.168.57.81:443/vsphere-client 
  17. Location: https://192.168.57.81:443/vsphere-client 
  18. < Connection: close 
  19. Connection: close 
  20.  
  21. <  
  22. * Closing connection #0 
  23. * Issue another request to this URL: 'https://192.168.57.81:443/vsphere-client' 
  24. * About to connect() to 192.168.57.81 port 443 (#0) 
  25. *   Trying 192.168.57.81... connected 
  26. * Connected to 192.168.57.81 (192.168.57.81) port 443 (#0) 
  27. * successfully set certificate verify locations: 
  28. *   CAfile: none 
  29.   CApath: /etc/ssl/certs 
  30. * SSLv3, TLS handshake, Client hello (1): 
  31. * SSLv3, TLS handshake, Server hello (2): 
  32. * SSLv3, TLS handshake, CERT (11): 
  33. * SSLv3, TLS alert, Server hello (2): 
  34. * SSL certificate problem, verify that the CA cert is OK. Details: 
  35. error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed 
  36. * Closing connection #0 
结束

更多欢迎到此讨论:

37275208

本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/886547



相关文章
|
2月前
|
测试技术 Go 开发工具
Go语言项目工程化 — 常见开发工具与 CI/CD 支持
Go语言项目工程化实践中的开发工具与CI/CD支持,涵盖格式化、静态检查、依赖管理、构建打包、自动化测试及部署策略。内容包括常用工具如gofmt、go vet、golangci-lint、Docker、GitHub Actions等,并提供实战建议与总结,提升团队协作效率与项目质量。
|
11月前
|
数据采集 存储 自然语言处理
快速构建企业智能门户,销售额倍增,人才触手可及 - 爬虫 + RAG + LLM
本文介绍了一款基于大模型的智能企业门户接待系统,旨在通过先进的AI技术,实现企业网站信息的自动化处理与响应,提高客户支持、产品推荐和人才招聘的效率。系统利用爬虫技术自动提取公司官网信息,结合语音识别、大模型生成等技术,支持语音和文本输入,通过RAG(检索增强生成)方式生成精准回答,并支持语音播报,提供类似真人的接待体验。项目涵盖了环境准备、数据构建、代码实现、测试调优、部署等多个阶段,详细记录了开发过程中遇到的问题及解决方案,展示了系统在咨询公司信息、产品询问及招聘岗位咨询等场景下的应用潜力。未来计划在数据类型支持、会话记忆、并发处理、语音合成等方面进一步优化,以提升用户体验和服务质量。
247 0
|
Prometheus 监控 Kubernetes
Prometheus 在微服务架构中的应用
【8月更文第29天】随着微服务架构的普及,监控和跟踪各个服务的状态变得尤为重要。Prometheus 是一个开源的监控系统和时间序列数据库,非常适合用于微服务架构中的监控。本文将详细介绍 Prometheus 如何支持微服务架构下的监控需求,包括服务发现、服务间的监控指标收集以及如何配置 Prometheus 来适应这些需求。
401 1
|
负载均衡 网络协议 关系型数据库
一口把LVS、Nginx及HAProxy工作原理讲清楚了。(附图)
一口把LVS、Nginx及HAProxy工作原理讲清楚了。(附图)
220 0
|
10月前
|
开发工具 C++ git
利用VS Code提升开发效率的五大插件推荐
本文推荐了五款能显著提升开发效率的VS Code插件:ESLint用于代码质量和风格检查;Prettier自动格式化代码;GitLens增强Git功能;Live Server提供前端实时预览;Docker支持容器管理。
|
机器学习/深度学习 监控 安全
IDS 和 IPS 的区别详解
【8月更文挑战第31天】
1044 0
|
11月前
|
人工智能 Serverless API
云原生应用开发平台CAP:一站式应用开发及生命周期管理解决方案
阿里云的云应用开发平台CAP(Cloud Application Platform)是一款一站式应用开发及应用生命周期管理平台。它提供丰富的Serverless与AI应用模板、高效的开发者工具链及企业级应用管理功能,帮助开发者快速构建、部署和管理云上应用,大幅提升研发、部署和运维效能。
828 4
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于策略的访问控制
这篇文章介绍了HAProxy的高级配置选项,特别是如何使用ACL(访问控制列表)进行基于策略的访问控制,通过实战案例展示了如何配置HAProxy以允许或拒绝来自特定源地址的访问。
206 6
HAProxy的高级配置选项-ACL篇之基于策略的访问控制
|
应用服务中间件 PHP Apache
HAProxy的高级配置选项-ACL篇之匹配访问路径案例
这篇文章介绍了HAProxy的高级配置选项,特别是如何使用ACL(访问控制列表)匹配访问路径以实现不同请求路径的流量分发到不同后端服务器的案例,通过实战配置展示了如何基于URL路径将请求定向到处理静态或动态内容的服务器。
235 5
HAProxy的高级配置选项-ACL篇之匹配访问路径案例
|
Kubernetes Cloud Native 关系型数据库
云原生数据基础设施之kubeblocks
云原生数据基础设施之kubeblocks