马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
章节
一、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设置选项
- acl <aclname> <criterion> [flags] [operator] [<value>]
- acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
复制代码 2.1 ACL 格式实例
- acl test hdr_dom(host) -i www.shuyan.com
-
- # ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,
- # 并且严格区分大小写,比如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 含糊匹配示例
- [root@localhost ~]# curl 192.168.239.10 -v
- * About to connect() to 192.168.239.10 port 80 (#0)
- * Trying 192.168.239.10...
- * Connected to 192.168.239.10 (192.168.239.10) port 80 (#0)
- > GET / HTTP/1.1
- > User-Agent: curl/7.29.0
- > Host: 192.168.239.10
- > Accept: */*
- >
- < HTTP/1.1 200 OK
- < Server: nginx/1.20.1
- < Date: Sat, 10 Aug 2024 09:59:32 GMT
- < Content-Type: text/html
- < Content-Length: 13
- < Last-Modified: Wed, 07 Aug 2024 08:08:45 GMT
- < Connection: keep-alive
- < ETag: "66b32b8d-d"
- < Accept-Ranges: bytes
- <
- this is web1
- * Connection #0 to host 192.168.239.10 left intact
- #示例:
- hdr(<string>) 用于测试请求头部首部指定内容如 GET、User-Agent、Host、Accept
- hdr_dom(host) 请求的host名称,如 www.openlab.com
- hdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.
- hdr_end(host) 请求的host结尾,如 .com .net .cn
复制代码
2.2.2 base : string
base 匹配的是哀求的 URL,包罗协议、用户名、暗码、主机名、端口和路径
- http://example.com/static/images/image.jpg?param1=value1¶m2=value2#fragment
复制代码 base 匹配的是 http://example.com/static/images/image.jpg 之中
- #返回第一个主机头和请求的路径部分的连接,该请求从第一个斜杠开始,并在问号之前结束,对虚拟主机有用
- <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
- # 提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
- <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子字符串匹配- #示例:
- path_beg -i /haproxy-status/
- path_end .jpg .jpeg .png .gif
- path_reg ^/images.*\.jpeg$
- path_sub image
- path_dir jpegs
- path_dom openlab
复制代码
对整个URL举行匹配
匹配范例形貌url正确字符串匹配url_beg前缀匹配url_dir子目次匹配url_dom域名匹配url_end后缀匹配url_len长度匹配url_reg正则表达式匹配url_sub子字符串匹配
2.3 ACL-flags
ACL匹配模式
- -i 不区分大小写
- -m 指定正则的匹配方法
- -n 不做DNS解析
- -u 禁止acl重名,否则多个同名ACL匹配或关系
复制代码
2.4 ACL-operator
ACL 利用符
- 整数比较:eq、ge、gt、le、lt
- 字符比较:
- - exact match (-m str) :字符串必须完全匹配模式
- - substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配
- - prefix match (-m beg) :在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配
- - suffix match (-m end) :将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配
- - subdir match (-m dir) :查看提取出来的用斜线分隔(“/”)的字符串,如其中任一个匹配,则ACL进行匹配
- - domain match (-m dom) :查找提取的用点(“.”)分隔字符串,如果其中任何一个匹配,则ACL进行匹配
复制代码
3 ACL案例
3.1 head
hdr_dom(host)
- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- acl test hdr_dom(host) -i www.shuyan.com # 只匹配域名
- use_backend webcluster-host if test
- default_backend default-host
- backend webcluster-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- backend default-host
- mode http
- server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
- # acl test hdr_dom(host) -i www.shuyan.com:这行配置定义了一个条件判断语句,如果请求的 Host 头
- # 部等于 www.shuyan.com,则满足条件。
- # use_backend webcluster-host if test:这行配置指定了如果满足条件,则将请求转发到
- # webcluster-host 后端。
- # default_backend default-host:这行配置指定了如果不满足任何条件,则将请求转发到
- # default-host 后端。
- # 客户端测试,添加域名解析
- [root@localhost ~]# vim /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.239.100 www.shuyan.com
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
复制代码 hdr_sub(host)
- # 使用sub模糊匹配shuyan
- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- acl test hdr_sub(host) -i shuyan # 包含shuyan字段的都可以访问,不区分大小写
- use_backend webcluster-host if test
- default_backend default-host
- backend webcluster-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- backend default-host
- mode http
- server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
- [root@haproxy ~]# systemctl restart haproxy.service
- # 客户端增加域名解析shuyan
- [root@localhost ~]# vim /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.239.100 www.shuyan.com shuyan
- # 测试
- [root@localhost ~]# curl shuyan
- this is web1
- [root@localhost ~]# curl shuyan
- this is web1
- [root@localhost ~]# curl shuyan
- this is web1
- [root@localhost ~]# curl shuyan
- this is web1
复制代码
hdr_beg(host)
- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- acl test hdr_beg(host) -m beg www # 以www开头都可以匹配
- [root@haproxy ~]# systemctl restart haproxy.service
- # 客户端测试
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# vim /etc/hosts
- [root@localhost ~]# curl shuyan
- this is web2
- [root@localhost ~]# curl shuyan
- this is web2
复制代码 hdr_end(host)
- # hdr_end 以什么什么结尾
- frontend webcluster
- bind *:80
- mode http
- acl test hdr_end(host) -m end com # 以com结尾的都可以匹配
- [root@haproxy ~]# systemctl restart haproxy.service
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl shuyan
- this is web2
- [root@localhost ~]# curl shuyan
- this is web2
- [root@localhost ~]# vim /etc/hosts
- [root@localhost ~]# curl shuyan.com
- this is web1
- [root@localhost ~]# curl shuyan.com
- this is web1
复制代码
3.2 base
- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- # 只要传过来的base存在shuyan都能被调度
- acl static base_sub -m sub shuyan
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- backend static-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- [root@haproxy ~]# systemctl restart haproxy.service
- # 客户端测试
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
- [root@localhost ~]# curl www.shuyan.com
- this is web1
复制代码
3.3 path
对以下路径举行匹配
- frontend webcluster
- bind *:80
- mode http
- #acl test hdr_end(host) -m end com
- acl static base_sub shuyan
- acl dynamics base_sub -m dir /php/
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- backend static-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- backend dynamics-host
- mode http
- server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
- # 下载PHP模块
- [root@web2 ~]# yum install php
- [root@web2 ~]# systemctl restart httpd
- [root@web2 ~]# mkdir -p /web/php/
- [root@web2 ~]# vim /web/php/index.php
- <?php
- phpinfo();
- ?>
复制代码 欣赏器测试

示例2

- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- #acl test hdr_end(host) -m end com
- # 只匹配路径部分的static不会关心前面域名到底是否正确,只要域名解析出来就可以了
- acl static path_sub -m sub static
- acl dynamics path_sub -m dir /php/
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- backend static-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- backend dynamics-host
- mode http
- server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
- [root@haproxy ~]# systemctl restart haproxy.service
- # NGINX部分代码
- server{
- listen 80 ;
- server_name www.shuyan.com;
- location / {
- root /web;
- index index.html;
- }
- }
- [root@web1 ~]# ls /web/static/
- index.html
- # 客户端
- [root@localhost ~]# vim /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.239.100 www.shuyan.com shuyan shuyan.com
- [root@localhost ~]# curl shuyan.com/static/
- this is web1
- [root@localhost ~]# curl shuyan.com/static/
- this is web1
- [root@localhost ~]# curl shuyan.com/static/
- this is web1
复制代码
3.4 基于源地点的访问控制
3.4.1 ACL-基于源地点的访问控制(src)
src 是 HAProxy 的一个匹配范例,用于基于源地点举行访问控制。使用 src 来限定哪些 IP 地点可以访问你的服务
只有源地点为192.168.239.14这台主性能访问
- frontend webcluster
- bind *:80
- mode http
- #acl test hdr_end(host) -m end com
- acl static hdr_sub(host) -m sub www.shuyan
- acl dynamics path_sub -m dir /php/
- acl ip_for src 192.168.239.14
- use_backend static-host if static ip_for
- use_backend dynamics-host if dynamics
- default_backend dynamics-host
复制代码
3.4.2 http-request deny 参数(拒绝访问)
http-request deny 是 HAProxy 的一个下令,用于拒绝 HTTP 哀求。它的作用是在满足肯定条件的环境下克制哀求继承处置惩罚,通常用于安全或访问控制目的。
- frontend webcluster
- bind *:80
- mode http
- #acl test hdr_end(host) -m end com
- acl static hdr_sub(host) -m sub www.shuyan
- acl dynamics path_sub -m dir /php/
- acl ip_for src 192.168.239.14
- http-request deny if ip_for
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- default_backend dynamics-host
复制代码
3.4.3 ACL-匹配欣赏器范例
将 wget curl 给禁用掉
- frontend webcluster
- bind *:80
- mode http
- acl static hdr_sub(User-Agent) -i wget curl
- acl dynamics path_sub -m dir /php/
- http-request deny if static
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- default_backend dynamics-host
复制代码 禁用了wget 和 curl 访问不了
欣赏器可以访问
3.5 ACL-基于文件后缀名实现动静分离
- # haproxy配置
- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- acl static path_sub -m end .html .png .jpg
- acl dynamics path_sub -m end .php
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- backend static-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- backend dynamics-host
- mode http
- server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
- # web1 -- NGINX配置
- [root@web1 ~]# ls /web/
- index.html static
- [root@web1 ~]# vim /etc/nginx/conf.d/aa.conf
- server{
- listen 80;
- server_name www.shuyan.com;
- location / {
- root /web;
- index index.html;
- }
- }
- [root@web1 ~]# systemctl restart nginx
- # web2 -- httpd配置
- [root@web2 ~]# vim /web/php/index.php
- <?php
- phpinfo();
- ?>
- [root@web2 ~]# vim /etc/httpd/conf.d/newvhosts.conf
- <VirtualHost 192.168.239.20:80>
- DocumentRoot /web/php
- ServerName 198.168.239.20
- </VirtualHost>
- <Directory /web>
- AllowOverride none
- Require all granted
- </Directory>
复制代码
实现效果:访问同一域名大概访问同一IP实现访问差别web服务器,如许的效果就叫做动静分离
访问index.php
访问index.html
3.6 ACL-匹配访问路径实现动静分离
- # haproxy 配置
- [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
- frontend webcluster
- bind *:80
- mode http
- acl static path_sub -m dir static
- acl dynamics path_sub -m dir php
- use_backend static-host if static
- use_backend dynamics-host if dynamics
- default_backend dynamics-host
- backend static-host
- mode http
- server web1 192.168.239.10:80 check inter 2 fall 2 rise 5
- backend dynamics-host
- mode http
- server web2 192.168.239.20:80 check inter 2 fall 2 rise 5
- [root@haproxy ~]# systemctl restart haproxy.service
- # web1 配置
- [root@web1 ~]# tree /web/
- /web/
- ├── index.html
- └── static
- └── index.html
- [root@web1 ~]# vim /etc/nginx/conf.d/aa.conf
- server{
- listen 80 ;
- server_name www.shuyan.com;
- location / {
- root /web;
- index index.html;
- }
- }
- # web2 配置
- [root@web2 ~]# vim /etc/httpd/conf.d/newvhosts.conf
- <VirtualHost 192.168.239.20:80>
- DocumentRoot /web
- ServerName 198.168.239.20
- </VirtualHost>
- <Directory /web>
- AllowOverride none
- Require all granted
- </Directory>
- [root@web2 ~]# systemctl restart httpd
复制代码 实现效果:根据路径来实现动静分离的效果
访问静态网页
访问动态网页
4 自界说HAProxy错误界面
当服务器down机的时间会出现以下界面,如许会显得整个界面及其不优雅
对指定的报错举行重定向,举行优雅的体现错误页面实现以下的效果
4.1 自界说页面内容(errorfil)
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service
自界说页面内容
- [root@haproxy ~]# vim /usr/share/haproxy/503.http
- HTTP/1.0 503 Service Unavailable
- Cache-Control: no-cache
- Connection: close
- Content-Type: text/html
- <html>
- <meta charset="UTF-8">
- <body><h1>我是你爹 Service Unavailable</h1>
- No server is available to handle this request.
- </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
- frontend mysql
- bind *:3306
- mode tcp
- use_backend mysqlservers
- backend mysqlservers
- mode tcp
- balance leastconn
- server mysql1 192.168.239.10:3306 check inter 3 fall 3 rise 5
- server mysql2 192.168.239.20:3306 check inter 3 fall 3 ries 5
复制代码 frontend 绑定了 *:3306,体现监听全部 IP 地点上的 MySQL 数据库端口。这意味着任何实验毗连到 *:3306 的客户端都会被转发到 mysqlservers 后端。
到mysql官方网站下载专属的yum堆栈
客户端、web服务器都须要下载MySQL,以下是利用
- # 下载官方MySQL的yum仓库
- [root@localhost yum.repos.d]# yum install https://dev.mysql.com/get/mysql84-community-release-el7-1.noarch.rpm
- # 下载MySQL
- [root@localhost yum.repos.d]# yum install mysql-server
- # 启动MySQL
- [root@localhost yum.repos.d]# systemctl start mysqld
- # 查看初始数据库密码
- [root@localhost yum.repos.d]# grep password /var/log/mysqld.log
- 2024-08-11T09:42:19.246928Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: n_vRmPY,Z0wW
- # 登录数据库
- [root@localhost yum.repos.d]# mysql -uroot -pn_vRmPY,Z0wW
- # 修改数据库密码,不然不让登录
- mysql> ALTER USER 'root'@'localhost' identified by 'Openlab123!';
- # 两台web服务器需要创建用户,并开启远程登录
- mysql> create user 'shuyan'@'%' identified by 'Openlab123!';
- Query OK, 0 rows affected (0.05 sec)
- # 权限增加
- mysql> GRANT ALL ON *.* TO 'shuyan'@'%';
- # 刷新权限表
- mysql> flush privileges;
复制代码 实现效果
在客户端上利用使用 -e 参数不登录输出
- [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | hostname | web1 |
- +---------------+-------+
- [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | hostname | web2 |
- +---------------+-------+
- [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | hostname | web1 |
- +---------------+-------+
- [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | hostname | web2 |
- +---------------+-------+
- [root@localhost yum.repos.d]# mysql -ushuyan -pOpenlab123! -h 192.168.239.100 -e "SHOW VARIABLES LIKE 'hostname'"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | hostname | web1 |
- +---------------+-------+
复制代码 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 文件。
- listen web-https
- bind *:80
- bind *:443 ssl crt /etc/haproxy/certs/shuyan.pem
- mode http
- redirect scheme https if !{ ssl_fc }
- balance roundrobin
- server web1 192.168.239.10:80 check inter 3 fall 3 rise 5
- 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
- [root@localhost yum.repos.d]# curl -IkL http://www.shuyan.com
- HTTP/1.1 302 Found
- content-length: 0
- location: https://www.shuyan.com/
- cache-control: no-cache
- HTTP/1.1 200 OK
- server: nginx/1.20.1
- date: Sun, 11 Aug 2024 16:37:31 GMT
- content-type: text/html
- content-length: 13
- last-modified: Sun, 11 Aug 2024 15:09:18 GMT
- etag: "66b8d41e-d"
- accept-ranges: bytes
- [root@localhost yum.repos.d]# curl -IkL http://www.shuyan.com
- HTTP/1.1 302 Found
- content-length: 0
- location: https://www.shuyan.com/
- cache-control: no-cache
- HTTP/1.1 200 OK
- server: nginx/1.20.1
- date: Sun, 11 Aug 2024 16:38:48 GMT
- content-type: text/html
- content-length: 13
- last-modified: Sun, 11 Aug 2024 15:09:18 GMT
- etag: "66b8d41e-d"
- accept-ranges: bytes
复制代码
|