haproxy之ACL

[复制链接]
发表于 2026-2-9 23:00:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
章节
一、haproxy 底子用法

二、haproxy 高级用法

三、haproxy之ACL的使用

目次
1 ACL先容

2 ACL设置选项

2.1 ACL 格式实例

2.2 ACL-criterion

2.2.1 head

 2.2.2 base : string

2.2.3 path : string

2.3 ACL-flags

2.4 ACL-operator

3 ACL案例

3.1 head

3.2 base

3.3 path 

3.4 基于源地点的访问控制

3.4.1 ACL-基于源地点的访问控制(src)

3.4.2 http-request deny 参数(拒绝访问)

3.4.3 ACL-匹配欣赏器范例

3.5 ACL-基于文件后缀名实现动静分离


3.6 ACL-匹配访问路径实现动静分离


4 自界说HAProxy错误界面

4.1 自界说页面内容(errorfil)

4.2 跳转重定向( errorloc)

5 HAProxy实现四层负载

5.1 四层和七层负载的先容和区别

5.2 四层与七层的优缺点


6 HAProxy- https实现


1 ACL先容

访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技能,它可以根据设定的条件对颠末服务器传输的数据包举行过滤(条件匹配),即对吸收到的报文举行匹配和过滤,基于哀求报文头部中的源地点、源端口、目的地点、目的端口、哀求方法、URL、文件后缀等信息内容举行匹配并实验进一步利用,好比允许其通过或抛弃。

2 ACL设置选项

  1. acl   <aclname>  <criterion>   [flags]     [operator]    [<value>]
  2. acl      名称      匹配规范      匹配模式     具体操作符     操作对象类型
复制代码
2.1 ACL 格式实例

  1. acl  test hdr_dom(host)   -i   www.shuyan.com
  2. ​
  3. # ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,
  4. # 并且严格区分大小写,比如Image_site和image_site就是两个完全不同的acl
复制代码

2.2 ACL-criterion


ACL的界说规范,判断条件
2.2.1 head

匹配范例形貌hdr完全匹配字符串,header 的指定信息hdr_beg前缀匹配,header 中指定匹配内容的 beginhdr_end后缀匹配,header 中指定匹配内容 endhdr_dom域匹配,header 中的 domain namehdr_dir路径匹配,header 的 uri 路径hdr_len长度匹配,header 的长度匹配hdr_reg正则表达式匹配,自界说表达式 (regex) 含糊匹配hdr_sub子串匹配,header 中的 uri 含糊匹配示例
  1. [root@localhost ~]# curl 192.168.239.10 -v
  2. * About to connect() to 192.168.239.10 port 80 (#0)
  3. *   Trying 192.168.239.10...
  4. * Connected to 192.168.239.10 (192.168.239.10) port 80 (#0)
  5. > GET / HTTP/1.1
  6. > User-Agent: curl/7.29.0
  7. > Host: 192.168.239.10
  8. > Accept: */*
  9. >
  10. < HTTP/1.1 200 OK
  11. < Server: nginx/1.20.1
  12. < Date: Sat, 10 Aug 2024 09:59:32 GMT
  13. < Content-Type: text/html
  14. < Content-Length: 13
  15. < Last-Modified: Wed, 07 Aug 2024 08:08:45 GMT
  16. < Connection: keep-alive
  17. < ETag: "66b32b8d-d"
  18. < Accept-Ranges: bytes
  19. <
  20. this is web1
  21. * Connection #0 to host 192.168.239.10 left intact
  22. #示例:
  23. hdr(<string>)     用于测试请求头部首部指定内容如 GET、User-Agent、Host、Accept
  24. hdr_dom(host)   请求的host名称,如 www.openlab.com
  25. hdr_beg(host)   请求的host开头,如 www.   img.   video.   download.   ftp.
  26. hdr_end(host)   请求的host结尾,如 .com   .net   .cn
复制代码

 2.2.2 base : string

   
  base 匹配的是哀求的 URL,包罗协议、用户名、暗码、主机名、端口和路径
  1. http://example.com/static/images/image.jpg?param1=value1&param2=value2#fragment
复制代码
base 匹配的是 http://example.com/static/images/image.jpg 之中
  1. #返回第一个主机头和请求的路径部分的连接,该请求从第一个斜杠开始,并在问号之前结束,对虚拟主机有用
  2. <scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>
复制代码
匹配范例形貌base正确字符串匹配base_beg前缀匹配base_dir子目次匹配base_dom域名匹配base_end后缀匹配base_len长度匹配base_reg正则表达式匹配base_sub子字符串匹配
2.2.3 path : string

  1. # 提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
  2. <scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
复制代码
也就是说path只匹配/<path>;<params>部分

匹配范例形貌path正确字符串匹配path_beg前缀匹配(哀求的 URL 开头,如 /static、/images、/img、/css)path_end后缀匹配(哀求的 URL 中资源的末端,如 .gif、.png、.css、.js、.jpg、.jpeg)path_dom域名匹配path_dir子目次匹配path_len长度匹配path_reg正则表达式匹配path_sub子字符串匹配
  1. #示例:
  2.     path_beg -i /haproxy-status/
  3.     path_end .jpg .jpeg .png .gif
  4.     path_reg ^/images.*\.jpeg$
  5.     path_sub image  
  6.     path_dir jpegs
  7.     path_dom openlab
复制代码

对整个URL举行匹配
匹配范例形貌url正确字符串匹配url_beg前缀匹配url_dir子目次匹配url_dom域名匹配url_end后缀匹配url_len长度匹配url_reg正则表达式匹配url_sub子字符串匹配
2.3 ACL-flags

ACL匹配模式
  1. -i 不区分大小写
  2. -m 指定正则的匹配方法
  3. -n 不做DNS解析
  4. -u 禁止acl重名,否则多个同名ACL匹配或关系
复制代码

2.4 ACL-operator

ACL 利用符
  1. 整数比较:eq、ge、gt、le、lt
  2. 字符比较:
  3. - exact match     (-m str) :字符串必须完全匹配模式
  4. - substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配
  5. - prefix match    (-m beg) :在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配
  6. - suffix match    (-m end) :将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配
  7. - subdir match    (-m dir) :查看提取出来的用斜线分隔(“/”)的字符串,如其中任一个匹配,则ACL进行匹配
  8. - domain match    (-m dom) :查找提取的用点(“.”)分隔字符串,如果其中任何一个匹配,则ACL进行匹配
复制代码

3 ACL案例

3.1 head

hdr_dom(host)
  1. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend webcluster
  3.     bind *:80
  4.     mode http
  5.     acl test hdr_dom(host) -i www.shuyan.com    # 只匹配域名
  6.     use_backend webcluster-host if test
  7.     default_backend default-host
  8. backend webcluster-host
  9.     mode http
  10.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  11. backend default-host
  12.     mode http
  13.     server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
  14. # acl test hdr_dom(host) -i www.shuyan.com:这行配置定义了一个条件判断语句,如果请求的 Host 头
  15. # 部等于 www.shuyan.com,则满足条件。
  16. # use_backend webcluster-host if test:这行配置指定了如果满足条件,则将请求转发到
  17. # webcluster-host 后端。
  18. # default_backend default-host:这行配置指定了如果不满足任何条件,则将请求转发到
  19. # default-host 后端。
  20. # 客户端测试,添加域名解析
  21. [root@localhost ~]# vim /etc/hosts
  22. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  23. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  24. 192.168.239.100  www.shuyan.com
  25. [root@localhost ~]# curl www.shuyan.com
  26. this is web1
  27. [root@localhost ~]# curl www.shuyan.com
  28. this is web1
  29. [root@localhost ~]# curl www.shuyan.com
  30. this is web1
  31. [root@localhost ~]# curl www.shuyan.com
  32. this is web1
  33. [root@localhost ~]# curl www.shuyan.com
  34. this is web1
  35. [root@localhost ~]# curl www.shuyan.com
  36. this is web1
  37. [root@localhost ~]# curl www.shuyan.com
  38. this is web1
  39. [root@localhost ~]# curl www.shuyan.com
复制代码
hdr_sub(host)
  1. # 使用sub模糊匹配shuyan
  2. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  3. frontend webcluster
  4.     bind *:80
  5.     mode http
  6.     acl test hdr_sub(host) -i shuyan     # 包含shuyan字段的都可以访问,不区分大小写
  7.     use_backend webcluster-host if test
  8.     default_backend default-host
  9. backend webcluster-host
  10.     mode http
  11.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  12. backend default-host
  13.     mode http
  14.     server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
  15. [root@haproxy ~]# systemctl restart haproxy.service
  16. # 客户端增加域名解析shuyan
  17. [root@localhost ~]# vim /etc/hosts
  18. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  19. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  20. 192.168.239.100 www.shuyan.com shuyan
  21. # 测试
  22. [root@localhost ~]# curl shuyan
  23. this is web1
  24. [root@localhost ~]# curl shuyan
  25. this is web1
  26. [root@localhost ~]# curl shuyan
  27. this is web1
  28. [root@localhost ~]# curl shuyan
  29. this is web1
复制代码


hdr_beg(host)
  1. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend webcluster
  3.     bind *:80
  4.     mode http
  5.     acl test hdr_beg(host) -m beg www     # 以www开头都可以匹配
  6. [root@haproxy ~]# systemctl restart haproxy.service
  7. # 客户端测试
  8. [root@localhost ~]# curl www.shuyan.com
  9. this is web1
  10. [root@localhost ~]# curl www.shuyan.com
  11. this is web1
  12. [root@localhost ~]# curl www.shuyan.com
  13. this is web1
  14. [root@localhost ~]# curl www.shuyan.com
  15. this is web1
  16. [root@localhost ~]# vim /etc/hosts
  17. [root@localhost ~]# curl shuyan
  18. this is web2
  19. [root@localhost ~]# curl shuyan
  20. this is web2
复制代码
hdr_end(host)
  1. # hdr_end 以什么什么结尾
  2. frontend webcluster
  3.     bind *:80
  4.     mode http
  5.     acl test hdr_end(host) -m end com   # 以com结尾的都可以匹配
  6. [root@haproxy ~]# systemctl restart haproxy.service
  7. [root@localhost ~]# curl www.shuyan.com
  8. this is web1
  9. [root@localhost ~]# curl www.shuyan.com
  10. this is web1
  11. [root@localhost ~]# curl www.shuyan.com
  12. this is web1
  13. [root@localhost ~]# curl shuyan
  14. this is web2
  15. [root@localhost ~]# curl shuyan
  16. this is web2
  17. [root@localhost ~]# vim /etc/hosts
  18. [root@localhost ~]# curl shuyan.com
  19. this is web1
  20. [root@localhost ~]# curl shuyan.com
  21. this is web1
复制代码

3.2 base


  1. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend webcluster
  3.     bind *:80
  4.     mode http
  5.     # 只要传过来的base存在shuyan都能被调度
  6.     acl static  base_sub -m sub  shuyan
  7.     use_backend static-host if static
  8.     use_backend dynamics-host if dynamics
  9. backend static-host
  10.     mode http
  11.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  12. [root@haproxy ~]# systemctl restart haproxy.service
  13. # 客户端测试
  14. this is web1
  15. [root@localhost ~]# curl www.shuyan.com
  16. this is web1
  17. [root@localhost ~]# curl www.shuyan.com
  18. this is web1
  19. [root@localhost ~]# curl www.shuyan.com
  20. this is web1
  21. [root@localhost ~]# curl www.shuyan.com
  22. this is web1
复制代码

3.3 path 

对以下路径举行匹配


  1. frontend webcluster
  2.     bind *:80
  3.     mode http
  4.     #acl  test hdr_end(host) -m end com
  5.     acl static  base_sub  shuyan
  6.     acl dynamics base_sub -m dir  /php/
  7.     use_backend static-host if static
  8.     use_backend dynamics-host if dynamics
  9. backend static-host
  10.     mode http
  11.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  12. backend dynamics-host
  13.     mode http
  14.     server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
  15. # 下载PHP模块
  16. [root@web2 ~]# yum install php
  17. [root@web2 ~]# systemctl restart httpd
  18. [root@web2 ~]# mkdir -p /web/php/
  19. [root@web2 ~]# vim /web/php/index.php
  20. <?php
  21.     phpinfo();
  22. ?>
复制代码
欣赏器测试



示例2

  1. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend webcluster
  3.     bind *:80
  4.     mode http
  5.     #acl  test hdr_end(host) -m end com
  6.     # 只匹配路径部分的static不会关心前面域名到底是否正确,只要域名解析出来就可以了
  7.     acl static  path_sub -m sub static
  8.     acl dynamics path_sub -m dir /php/
  9.     use_backend static-host if static
  10.     use_backend dynamics-host if dynamics
  11. backend static-host
  12.     mode http
  13.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  14. backend dynamics-host
  15.     mode http
  16.     server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
  17. [root@haproxy ~]# systemctl restart haproxy.service
  18. # NGINX部分代码
  19. server{
  20.   listen 80 ;
  21.   server_name www.shuyan.com;
  22.   location / {
  23.     root /web;
  24.     index index.html;
  25.   }
  26. }
  27. [root@web1 ~]# ls /web/static/
  28. index.html
  29. # 客户端
  30. [root@localhost ~]# vim /etc/hosts
  31. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  32. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  33. 192.168.239.100 www.shuyan.com shuyan shuyan.com
  34. [root@localhost ~]# curl shuyan.com/static/
  35. this is web1
  36. [root@localhost ~]# curl shuyan.com/static/
  37. this is web1
  38. [root@localhost ~]# curl shuyan.com/static/
  39. this is web1
复制代码

3.4 基于源地点的访问控制

3.4.1 ACL-基于源地点的访问控制(src)


src 是 HAProxy 的一个匹配范例,用于基于源地点举行访问控制。使用 src 来限定哪些 IP 地点可以访问你的服务

只有源地点为192.168.239.14这台主性能访问
  1. frontend webcluster
  2.     bind *:80
  3.     mode http
  4.     #acl  test hdr_end(host) -m end com
  5.     acl static  hdr_sub(host) -m sub www.shuyan
  6.     acl dynamics path_sub -m dir /php/
  7.     acl ip_for src 192.168.239.14
  8.     use_backend static-host if static ip_for
  9.     use_backend dynamics-host if dynamics
  10.     default_backend dynamics-host
复制代码


3.4.2 http-request deny 参数(拒绝访问)

http-request deny 是 HAProxy 的一个下令,用于拒绝 HTTP 哀求。它的作用是在满足肯定条件的环境下克制哀求继承处置惩罚,通常用于安全或访问控制目的。

  1. frontend webcluster
  2.     bind *:80
  3.     mode http
  4.     #acl  test hdr_end(host) -m end com
  5.     acl static  hdr_sub(host) -m sub www.shuyan
  6.     acl dynamics path_sub -m dir /php/
  7.     acl ip_for src 192.168.239.14
  8.     http-request deny if ip_for
  9.     use_backend static-host if static
  10.     use_backend dynamics-host if dynamics
  11.     default_backend dynamics-host
复制代码


3.4.3 ACL-匹配欣赏器范例

将 wget curl 给禁用掉


  1. frontend webcluster
  2.     bind *:80
  3.     mode http
  4.     acl static  hdr_sub(User-Agent) -i wget curl
  5.     acl dynamics path_sub -m dir /php/
  6.     http-request deny if static
  7.     use_backend static-host if static
  8.     use_backend dynamics-host if dynamics
  9.     default_backend dynamics-host
复制代码
禁用了wget 和 curl 访问不了

欣赏器可以访问


3.5 ACL-基于文件后缀名实现动静分离



  1. # haproxy配置
  2. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  3. frontend webcluster
  4.     bind *:80
  5.     mode http
  6.     acl static  path_sub -m end .html .png .jpg
  7.     acl dynamics path_sub -m end .php
  8.     use_backend static-host if static
  9.     use_backend dynamics-host if dynamics
  10. backend static-host
  11.     mode http
  12.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  13. backend dynamics-host
  14.     mode http
  15.     server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
  16. # web1 -- NGINX配置
  17. [root@web1 ~]# ls /web/
  18. index.html  static
  19. [root@web1 ~]# vim /etc/nginx/conf.d/aa.conf
  20. server{
  21.   listen 80;
  22.   server_name www.shuyan.com;
  23.   location / {
  24.     root /web;
  25.     index index.html;
  26.           }
  27.        }
  28. [root@web1 ~]# systemctl restart nginx
  29. # web2 -- httpd配置
  30. [root@web2 ~]# vim /web/php/index.php
  31. <?php
  32.     phpinfo();
  33. ?>
  34. [root@web2 ~]# vim /etc/httpd/conf.d/newvhosts.conf
  35. <VirtualHost 192.168.239.20:80>
  36.         DocumentRoot /web/php
  37.         ServerName 198.168.239.20
  38. </VirtualHost>
  39. <Directory /web>
  40.         AllowOverride none
  41.         Require all granted
  42. </Directory>
复制代码

实现效果:访问同一域名大概访问同一IP实现访问差别web服务器,如许的效果就叫做动静分离
访问index.php

访问index.html



3.6 ACL-匹配访问路径实现动静分离



  1. # haproxy 配置
  2. [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
  3. frontend webcluster
  4.     bind *:80
  5.     mode http
  6.     acl static  path_sub -m dir static
  7.     acl dynamics path_sub -m dir php
  8.     use_backend static-host if static
  9.     use_backend dynamics-host if dynamics
  10.     default_backend dynamics-host
  11. backend static-host
  12.     mode http
  13.     server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
  14. backend dynamics-host
  15.     mode http
  16.     server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
  17. [root@haproxy ~]# systemctl restart haproxy.service
  18. # web1 配置
  19. [root@web1 ~]# tree /web/
  20. /web/
  21. ├── index.html
  22. └── static
  23.     └── index.html
  24. [root@web1 ~]# vim /etc/nginx/conf.d/aa.conf
  25. server{
  26.   listen 80 ;
  27.   server_name www.shuyan.com;
  28.   location / {
  29.     root /web;
  30.     index index.html;
  31.           }
  32.       }
  33. # web2 配置
  34. [root@web2 ~]# vim /etc/httpd/conf.d/newvhosts.conf
  35. <VirtualHost 192.168.239.20:80>
  36.         DocumentRoot /web
  37.         ServerName 198.168.239.20
  38. </VirtualHost>
  39. <Directory /web>
  40.         AllowOverride none
  41.         Require all granted
  42. </Directory>
  43. [root@web2 ~]# systemctl restart httpd
复制代码
实现效果:根据路径来实现动静分离的效果
访问静态网页

访问动态网页




4 自界说HAProxy错误界面


当服务器down机的时间会出现以下界面,如许会显得整个界面及其不优雅

 对指定的报错举行重定向,举行优雅的体现错误页面实现以下的效果

4.1 自界说页面内容(errorfil)

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

[root@haproxy ~]# systemctl restart haproxy.service 
自界说页面内容
  1. [root@haproxy ~]# vim /usr/share/haproxy/503.http
  2. HTTP/1.0 503 Service Unavailable
  3. Cache-Control: no-cache
  4. Connection: close
  5. Content-Type: text/html
  6. <html>
  7. <meta charset="UTF-8">
  8. <body><h1>我是你爹 Service Unavailable</h1>
  9. No server is available to handle this request.
  10. </body></html>
复制代码
将 web1 服务器手动down机
[root@web1 ~]# systemctl stop nginx
欣赏器访问就可出现以下自界说的内容了


4.2 跳转重定向( errorloc)

当服务器down机的时间,访问本身的服务器将服务器重定向到百度

[root@haproxy ~]# systemctl restart haproxy.service 

访问不到直接跳转到百度


5 HAProxy实现四层负载

5.1 四层和七层负载的先容和区别

四层负载均衡器(Layer 4 Load Balancer)工作在网络传输层(TCP/UDP 层),它根据 IP 地点和端标语举行负载均衡。四层负载均衡器只能辨认网络层的信息,无法辨认应用层的数据包内容,因此不能举行高级的路由决议。
七层负载均衡器(Layer 7 Load Balancer)工作在网络应用层(HTTP/HTTPS 层),它可以或许辨认应用层的数据包内容,比方 HTTP 哀求的头部和正文。七层负载均衡器可以根据哀求的内容举行路由决议,比方根据 URL、哀求头、哀求体等信息决定将哀求转发到哪个后端服务器。
简而言之,四层负载均衡器只能根据 IP 地点和端标语举行负载均衡,而七层负载均衡器还可以根据应用层的信息举行负载均衡。因此,七层负载均衡器比四层负载均衡器更加机动和强大,但也更加复杂和昂贵。

5.2 四层与七层的优缺点

四层负载均衡器的长处是简单、高效和稳固。由于它只关心 IP 地点和端标语,因此可以快速地举行负载均衡,而且不会受到应用层的影响。别的,四层负载均衡器可以在任何应用步调上运行,无需相识应用层的具体细节。
七层负载均衡器的长处是可以根据应用层的信息举行负载均衡,提供更多的机动性和控制力。比方,你可以根据哀求的 URL、哀求头、哀求体等信息决定将哀求转发到哪个后端服务器。如许,你可以更好地优化性能和可用性,同时镌汰单点故障的风险。
总得来说就是四层负载均衡器简单、高效;七层负载均衡器机动、可控。


四层负载示例
留意:假如使用frontend和backend,肯定在frontend和backend段中都指定mode tcp
  1. frontend mysql
  2.     bind  *:3306
  3.     mode  tcp
  4.     use_backend  mysqlservers
  5. backend mysqlservers
  6.     mode  tcp
  7.     balance leastconn
  8.     server mysql1 192.168.239.10:3306 check inter 3 fall 3 rise 5
  9.     server mysql2 192.168.239.20:3306 check inter 3 fall 3 ries 5
复制代码
frontend 绑定了 *:3306,体现监听全部 IP 地点上的 MySQL 数据库端口。这意味着任何实验毗连到 *:3306 的客户端都会被转发到 mysqlservers 后端。
到mysql官方网站下载专属的yum堆栈

客户端、web服务器都须要下载MySQL,以下是利用

  1. # 下载官方MySQL的yum仓库
  2. [root@localhost yum.repos.d]# yum install https://dev.mysql.com/get/mysql84-community-release-el7-1.noarch.rpm
  3. # 下载MySQL
  4. [root@localhost yum.repos.d]# yum install mysql-server
  5. # 启动MySQL
  6. [root@localhost yum.repos.d]# systemctl start mysqld
  7. # 查看初始数据库密码
  8. [root@localhost yum.repos.d]# grep password /var/log/mysqld.log
  9. 2024-08-11T09:42:19.246928Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: n_vRmPY,Z0wW
  10. # 登录数据库
  11. [root@localhost yum.repos.d]# mysql -uroot -pn_vRmPY,Z0wW
  12. # 修改数据库密码,不然不让登录
  13. mysql> ALTER USER 'root'@'localhost' identified by 'Openlab123!';
  14. # 两台web服务器需要创建用户,并开启远程登录
  15. mysql> create user 'shuyan'@'%' identified by 'Openlab123!';
  16. Query OK, 0 rows affected (0.05 sec)
  17. # 权限增加
  18. mysql> GRANT ALL ON *.* TO 'shuyan'@'%';
  19. # 刷新权限表
  20. mysql> flush privileges;
复制代码
实现效果
在客户端上利用使用 -e 参数不登录输出
  1. [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +---------------+-------+
  4. | Variable_name | Value |
  5. +---------------+-------+
  6. | hostname      | web1  |
  7. +---------------+-------+
  8. [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
  9. mysql: [Warning] Using a password on the command line interface can be insecure.
  10. +---------------+-------+
  11. | Variable_name | Value |
  12. +---------------+-------+
  13. | hostname      | web2  |
  14. +---------------+-------+
  15. [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. +---------------+-------+
  18. | Variable_name | Value |
  19. +---------------+-------+
  20. | hostname      | web1  |
  21. +---------------+-------+
  22. [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
  23. mysql: [Warning] Using a password on the command line interface can be insecure.
  24. +---------------+-------+
  25. | Variable_name | Value |
  26. +---------------+-------+
  27. | hostname      | web2  |
  28. +---------------+-------+
  29. [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
  30. mysql: [Warning] Using a password on the command line interface can be insecure.
  31. +---------------+-------+
  32. | Variable_name | Value |
  33. +---------------+-------+
  34. | hostname      | web1  |
  35. +---------------+-------+
复制代码
6 HAProxy- https实现

   [root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/shuyan.key -x509 -days 365 -out /etc/haproxy/certs/shuyan.crt
   

  • req:告诉 OpenSSL 天生证书哀求。
  • -newkey rsa:2048:天生一个长度为 2048 位的 RSA 密钥对。
  • -nodes:告诉 OpenSSL 不要加密私钥。
  • -sha256:设置哈希算法为 SHA-256。
  • -keyout /etc/haproxy/certs/shuyan.key:指定私钥的输出文件。在这里,它是 /etc/haproxy/certs/shuyan.key。
  • -x509:告诉 OpenSSL 创建一个自署名证书。
  • -days 365:设置证书的有用期为 365 天。
  • -out /etc/haproxy/certs/shuyan.crt:指定证书的输出文件。在这里,它是 /etc/haproxy/certs/shuyan.crt。
   
  [root@haproxy ~]# cat /etc/haproxy/certs/shuyan.key /etc/haproxy/certs/shuyan.crt > /etc/haproxy/certs/shuyan.pem
  
  [root@haproxy ~]# ls /etc/haproxy/certs/
shuyan.crt  shuyan.key  shuyan.pem
  cat haproxy.key haproxy.crt > haproxy.pem 是一条下令,它将两个文件 haproxy.key(私钥)和 haproxy.crt(证书)归并成一个名为 haproxy.pem 的单个文件。这通常是须要的,由于像 HAProxy 如许的软件须要提供一个组合的 PEM 文件。

  1. listen web-https
  2.     bind *:80
  3.     bind *:443 ssl crt /etc/haproxy/certs/shuyan.pem
  4.     mode http
  5.     redirect scheme https if !{ ssl_fc }
  6.     balance roundrobin
  7.     server web1 192.168.239.10:80 check inter 3 fall 3 rise 5
  8.     server web2 192.168.239.20:80 check inter 3 fall 3 rise 5
复制代码
  

  • redirect scheme https: 这个指令告诉 HAProxy 将哀求重定向到 HTTPS 方案(纵然用端口 443)。
  • if !{ ssl_fc }: 这个条件判断查抄前端毗连 (ssl_fc) 是否没有使用 SSL/TLS 加密。!{ ssl_fc } 意味着假如前端毗连不是通过 SSL/TLS 创建的(即不是 HTTPS),那么就实验前面的重定向利用。

实现效果



使用http也能使用redirect起作用并重定向到https
  1. [root@localhost yum.repos.d]# curl -IkL http://www.shuyan.com
  2. HTTP/1.1 302 Found
  3. content-length: 0
  4. location: https://www.shuyan.com/
  5. cache-control: no-cache
  6. HTTP/1.1 200 OK
  7. server: nginx/1.20.1
  8. date: Sun, 11 Aug 2024 16:37:31 GMT
  9. content-type: text/html
  10. content-length: 13
  11. last-modified: Sun, 11 Aug 2024 15:09:18 GMT
  12. etag: "66b8d41e-d"
  13. accept-ranges: bytes
  14. [root@localhost yum.repos.d]# curl -IkL http://www.shuyan.com
  15. HTTP/1.1 302 Found
  16. content-length: 0
  17. location: https://www.shuyan.com/
  18. cache-control: no-cache
  19. HTTP/1.1 200 OK
  20. server: nginx/1.20.1
  21. date: Sun, 11 Aug 2024 16:38:48 GMT
  22. content-type: text/html
  23. content-length: 13
  24. last-modified: Sun, 11 Aug 2024 15:09:18 GMT
  25. etag: "66b8d41e-d"
  26. accept-ranges: bytes
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表