章节
目录
3.4.2 http-request deny 参数(拒绝访问)
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 中指定匹配内容的 begin |
hdr_end | 后缀匹配,header 中指定匹配内容 end |
hdr_dom | 域匹配,header 中的 domain name |
hdr_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