反向代理软件之HAproxy访问控制列表ACL与自定义错误页面,四层负载
访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条
件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头
部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行
进一步操作,比如允许其通过或丢弃。
官方帮助:
http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#7
http://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7
ACL配置选项
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
ACL-Name
acl image_service hdr_dom(host) -i img.magedu.com
#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大
小写,比如:my_acl和My_Acl就是两个完全不同的acl
ACL-criterion
定义ACL匹配规范,即:判断条件
hdr string,提取在一个HTTP请求报文的首部
hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出
现次数
hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin
hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end
hdr_dom([<name> [,<occ>]]):域匹配,header中的domain name
hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配
#示例:
hdr(<string>) 用于测试请求头部首部指定内容
hdr_dom(host) 请求的host名称,如 www.longxuan.vip,m.longxuan.vip
hdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.
hdr_end(host) 请求的host结尾,如 .com .net .cn
#示例:
acl bad_agent hdr_sub(User-Agent) -i curl wget
block if bad_agent
#有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www
acl short_form hdr_beg(host) www.
acl alternate1 hdr_beg(host) -m beg www.
acl alternate2 hdr_dom(host) -m beg www.
acl alternate3 hdr(host) -m beg www.
base : string
#返回第一个主机头和请求的路径部分的连接,该请求从主机名开始,并在问号之前结束,对虚拟主机有用,下
面的例子中是两个#中间的内容,实际#是没有的
<scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>
base : exact string match
base_beg : prefix match
base_dir : subdir match
base_dom : domain match
base_end : suffix match
base_len : length match
base_reg : regex match
base_sub : substring match
path : string
#提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
<scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
path : exact string match
path_beg : prefix match #请求的URL开头,如/static、/images、/img、/css
path_end : suffix match #请求的URL中资源的结尾,如 .gif .png .css .js .jpg
.jpeg
path_dom : domain match
path_dir : subdir match
path_len : length match
path_reg : regex match
path_sub : substring match
#示例:
path_beg -i /haproxy-status/
path_end .jpg .jpeg .png .gif
path_reg ^/images.*\.jpeg$
path_sub image
path_dir jpegs
path_dom longxuan
url : string
#提取请求中的整个URL。一个典型的应用是具有预取能力的缓存,以及需要从数据库聚合多个信息并将它们保
存在缓存中的网页门户入口,推荐使用path
url :exact string match
url_beg : prefix match
url_dir : subdir match
url_dom : domain match
url_end : suffix match
url_len : length match
url_reg : regex match
url_sub : substring match
src #源IP
src_port #源PORT
dst #目标IP
dst_port #目标PORT
#示例:
acl invalid_src src 172.31.0.7 192.168.1.0/24
acl invalid_src src 10.1.0.0/24
acl invalid_port src_port 0:1023
status : integer #返回在响应报文中的状态码
#七层协议
acl valid_method method GET HEAD
http-request deny if ! valid_method
ACL-flags
ACL匹配模式
-i 不区分大小写
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否则多个同名ACL匹配或关系
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进行匹配
ACL-value
value的类型
The ACL engine can match these types against patterns of the following types :
- Boolean #布尔值
- integer or integer range #整数或整数范围,比如用于匹配端口范围
- IP address / network #IP地址或IP范围, 192.168.0.1 ,192.168.0.1/24
- string--> www.longxuan.vip
exact #精确比较
substring #子串
suffix #后缀比较
prefix #前缀比较
subdir #路径, /wp-includes/js/jquery/jquery.js
domain #域名,www.longxuan.vip
- regular expression #正则表达式
- hex block #16进制
多个ACL的组合调用方式
多个ACL的逻辑处理
与:隐式(默认)使用
或:使用“or" 或 “||"表示
否定:使用 "!" 表示
多个ACL调用方式:
#示例:
if valid_src valid_port #与关系,ACL中A和B都要满足为true,默认为与
if invalid_src || invalid_port #或,ACL中A或者B满足一个为true
if ! invalid_src #非,取反,不满足ACL才为true
ACL示例-域名匹配
[root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl pc_domain hdr_dom(host) -i www.longxuan.vip
acl mobile_domain hdr_dom(host) -i mobile.longxuan.vip
###################### acl hosts #################################
use_backend pc_hosts if pc_domain
use_backend mobile_hosts if mobile_domain
default_backend pc_hosts #所有ACL都不匹配,则使用的默认backend
###################### backend hosts #############################
backend mobile_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend pc_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
测试结果:
[root@centos6 ~]# curl www.longxuan.vip
172.31.0.27
[root@centos6 ~]# curl mobile.longxuan.vip
172.31.0.17
[root@centos6 ~]# curl longxuan.vip
172.31.0.27
ACL示例-基于源IP或子网调度访问
将指定的源地址调度至指定的web服务器组。
root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl pc_domain hdr_dom(host) -i www.longxuan.vip
acl mobile_domain hdr_dom(host) -i mobile.longxuan.vip
acl ip_range_test src 10.18.0.0/16 172.31.0.16 #基于源地址的ACL,定义多个ACL的顺序无关
acl ip_range_test2 src 10.18.0.200
###################### acl hosts #################################
use_backend pc_hosts if ip_range_test #放在前面的ACL规则优先生效,引用ACL时,严格的ACL应放在前面
use_backend pc_hosts if pc_domain
use_backend mobile_hosts if mobile_domain
default_backend pc_hosts
###################### backend hosts #############################
backend mobile_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend pc_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
测试结果
[root@centos6 ~]# hostname -I
10.31.0.6
[root@centos6 ~]# curl www.lngxuan.vip
172.31.0.27
[root@internet ~]# curl -H "HOST: www.longxuan.vip" 172.31.0.7
172.31.0.27
[root@centos6 ~]# curl mobile.longxuan.vip
172.31.0.27
[root@centos6 ~]# curl longxuan.vip
172.31.0.27
[root@centos8 ~]# curl mobile.longxuan.vip
172.31.0.17
[root@centos8 ~]# curl www.longxuan.vip
172.31.0.27
[root@centos8 ~]# curl longxuan.vip
172.31.0.27
ACL示例-基于源地址的访问控制
拒绝指定IP或者IP范围访问
listen web_host
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl acl_deny_src src 172.31.0.6 192.168.0.0/24
###################### acl hosts #################################
http-request deny if acl_deny_src #2.1版本后,不再支持block
#block if acl_deny_src
#http-request allow
default_backend default_web
###################### backend hosts #############################
backend longxuan_host
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 172.31.0.27:80 check inter 2000 fall 3 rise 5
测试:
[root@centos6 ~]# curl www.longxuan.vip
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
ACL示例-匹配浏览器类型
匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组
范例: 163 网易拒绝curl和wget的访问
[root@ubuntu2004 ~]# curl -I www.163.com
HTTP/1.1 403 Forbidden
Date: Fri, 01 Jan 2021 06:57:22 GMT
[root@ubuntu2004 ~]# curl -I -A "wget" www.163.com
HTTP/1.1 403 Forbidden
Date: Fri, 01 Jan 2021 06:58:13 GMT
#不拒绝其它浏览器的访问
[root@ubuntu2004 ~]# curl -I -A "ApacheBench" www.163.com
HTTP/1.1 301 Moved Permanently
Date: Fri, 01 Jan 2021 06:58:41 GMT
范例:
[root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl acl_user_agent hdr_sub(User-Agent) -i curl wget #基于浏览器的ACL
acl acl_user_agent_ab hdr_sub(User-Agent) -i ApacheBench
###################### acl hosts #################################
redirect prefix http://www.baidu.com if acl_user_agent #302 临时重定向至新URL
http-request deny if acl_user_agent_ab #拒绝ab
default_backend pc_hosts
###################### backend hosts #############################
backend mobile_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend pc_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
范例:
[root@centos6 ~]# curl -I 172.31.0.7
HTTP/1.1 302 Found
content-length: 0
location: http://172.31.0.8/
cache-control: no-cache
[root@centos6 ~]# curl -L 172.31.0.7
172.31.0.8
[root@centos6 ~]# wget -O - -q http://172.31.0.7
172.31.0.8
[root@centos6 ~]#curl -A chrome http://172.31.0.7
172.31.0.27
#模拟ab
[root@centos6 ~]# curl -A ApacheBench 172.31.0.7
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@centos6 ~]# ab -n1 -c 1 http://172.31.0.7/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
#haproxy日志提示403
[root@centos7 ~]# tail /var/log/haproxy.log
Apr 4 08:16:29 localhost haproxy[1483]: 172.31.0.6:56470
ACL示例-基于文件后缀名实现动静分离
[root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html #基于文件后缀名的ACL
acl acl_php path_end -i .php
###################### acl hosts #################################
use_backend mobile_hosts if acl_static
use_backend app_hosts if acl_php
default_backend pc_hosts
###################### backend hosts #############################
backend mobile_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend pc_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
backend app_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
分别在后端两台主机准备相关文件
[root@centos17 ~]# ls /var/www/html
index.html long.jpg
[root@centos27 ~]# cat /var/www/html/test.php
<?php
echo "<h1>http://172.31.0.27/test.php</h1>\n";
?>
ACL-匹配访问路径实现动静分离
[root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl acl_static path_beg -i /static /images /javascript #基于路径的ACL
acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html .htm #ACL同名为或关系
acl acl_app path_beg -i /api
###################### acl hosts #################################
use_backend static_hosts if acl_static
use_backend app_hosts if acl_app
default_backend app_hosts
###################### backend hosts #############################
backend static_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend app_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
#创建相关文件
[root@centos17 ~]# mkdir /var/www/html/static
[root@centos17 ~]# echo 172.31.0.17 > /var/www/html/static/test.html
#测试访问
[root@centos6 ~]# curl 172.31.0.7/static/test.html
172.31.0.17
ACL示例-预定义ACL使用
官方帮助文档:http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#7.4
预定义ACL
ACL name|Equivalent to |Usage
---|---|---|---
FALSE|always_false|never match
HTTP |req_proto_http| match if protocol is valid HTTP
HTTP_1.0 |req_ver 1.0 |match HTTP version 1.0
HTTP_1.1 |req_ver 1.1| match HTTP version 1.1
HTTP_CONTENT |hdr_val(content-length) gt 0 |match an existing content-length
HTTP_URL_ABS| url_reg [/:]😕/ |match absolute URL with scheme
HTTP_URL_SLASH |url_beg / |match URL beginning with "/"
HTTP_URL_STAR |url * |match URL equal to ""
LOCALHOST |src 127.0.0.1/8 |match connection from local host
METH_CONNECT |method CONNECT |match HTTP CONNECT method
METH_DELETE| method DELETE |match HTTP DELETE method
METH_GET |method GET HEAD |match HTTP GET or HEAD method
METH_HEAD |method HEAD |match HTTP HEAD method
METH_OPTIONS |method OPTIONS| match HTTP OPTIONS method
METH_POST |method POST |match HTTP POST method
METH_PUT |method PUT |match HTTP PUT method
METH_TRACE |method TRACE |match HTTP TRACE method
RDP_COOKIE |req_rdp_cookie_cnt gt 0| match presence of an RDP cookie
REQ_CONTENT |req_len gt 0 |match data in the request buffer
TRUE |always_true |always match
WAIT_END |wait_end |wait for end of content analysis
预定义ACL使用
范例: 禁止TRACE方法和HTTP/1.1协议
[root@centos6 ~]# curl -I -XTRACE 172.31.0.7/static/test.html
HTTP/1.1 200 OK
date: Sat, 04 Apr 2021 02:04:01 GMT
[root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl acl_static_path path_beg -i /static /images /javascript
###################### acl hosts #################################
use_backend static_path_hosts if acl_static_path
http-request deny if METH_TRACE HTTP_1.1 #引用预定义的ACL,多个ACL默认为与关系
default_backend pc_hosts
################### backend hosts ################################
backend static_path_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend mobile_hosts
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend pc_hosts
mode http
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
[root@centos6 ~]# curl -I -XTRACE 172.31.0.7/static/test.html
HTTP/1.1 403 Forbidden
content-length: 93
[root@centos6 ~]# curl -I -0 -XTRACE 172.31.0.7/static/test.html
HTTP/1.1 200 OK
date: Sat, 04 Apr 2021 02:10:13 GMT
#查看日志,观察协议版本
[root@centos17 ~]# tail /var/log/httpd/access_log
172.31.0.7 - - [04/Apr/2021:10:11:41 +0800] "TRACE /static/test.html HTTP/1.0" 200230 "-" "curl/7.19.7
[root@centos6 ~]# curl -i 172.31.0.7/static/test.html
HTTP/1.1 200 OK
date: Sat, 04 Apr 2021 02:07:58 GMT
...
172.31.0.17
自定义HAProxy 错误界面
对指定的报错进行重定向,进行优雅的显示错误页面
使用errorfile和errorloc指令的两种方法,可以实现自定义各种错误页面
默认情况下,所有后端服务器都down机后,会显示下面页面为:
503 Service Unavailable
基于自定义的错误页面文件
#自定义错误页
errorfile <code> <file>
<code> #HTTP status code.支持200, 400, 403, 405, 408, 425, 429, 500, 502,503,504
<file> #包含完整HTTP响应头的错误页文件的绝对路径。 建议后缀".http",以和一般的html文件相区分
#示例:
errorfile 400 /etc/haproxy/errorfiles/400badreq.http
errorfile 403 /etc/haproxy/errorfiles/403forbid.http
errorfile 503 /etc/haproxy/errorfiles/503sorry.http
范例:
defaults
#option forwardfor
#no option http-use-htx 支持html文件,此设置和版本有关,2.1不支持
#......
#加下面行
errorfile 500 /usr/local/haproxy/html/500.http
errorfile 502 /usr/local/haproxy/html/502.http
errorfile 503 /usr/local/haproxy/html/503.http
范例:
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
errorfile 503 /apps/haproxy/html/503.http
listen
.......
[root@centos7 ~]# vim /apps/haproxy/html/503.http
HTTP/1.1 503 Service Unavailable
Content-Type:text/html;charset=utf-8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中......请稍候再试</h1></center>
<center><h2>联系电话:400-123-8888</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
[root@centos7 ~]# systemctl restart haproxy
#将后端服务器down,可以观察到以下页面
范例:启用 no option http-use-htx
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
defaults
option http-keep-alive
no option http-use-htx #在defaults 块中添加
errorfile 503 /apps/haproxy/errorfiles/503.html
[root@haproxy ~]# cat /apps/haproxy/errorfiles/503.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中......请稍侯再试</h1></center>
<center><h2>联系电话:400-123-8888</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
#注意没有响应头信息
[root@internet ~]# curl -I 10.1.0.100
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中......请稍侯再试</h1></center>
<center><h2>联系电话:400-123-8888</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
#ubuntu的客户端提示错误
root@ubuntu2004:~# curl 10.1.0.100
curl: (1) Received HTTP/0.9 when not allowed
基于http重定向错误页面
#错误页面重定向
errorloc <code> <url>
#相当于errorloc302 <code> <url>,利用302重定向至指URL
#示例:
errorloc 503 http://www.longxuan.vip/error_pages/503.html
errorloc 503 http://www.longxuan.vip/
范例:
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg
defaults
#option http-keep-alive
#option forwardfor
#no option http-use-htx
#...... 加以下一行
#errorfile 503 /apps/haproxy/html/503.http
errorloc 503 http://172.31.0.8/error_page/503.html
[root@centos8 ~]# cat /var/www/html/error_page/503.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中......请稍侯再试</h1></center>
<center><h2>联系电话:400-123-8839</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
#浏览器访问http://haproxy/ 302自动跳转至下面页面:
网站维护中......请稍侯再试
联系电话:400-123-8839
HAProxy 四层负载
针对除HTTP以外的TCP协议应用服务访问的应用场景
MySQL
Redis
Memcached
RabbitMQ
问题: 后端服务器到底是与haproxy还是和客户端建立三次握手呢?
四层负载示例
注意:如果使用frontend和backend,一定在 frontend 和 backend 段中都指定mode tcp
listen redis-port
bind 172.31.0.7:6379
mode tcp
balance leastconn
server server1 172.31.0.17:6379 check
server server2 172.31.0.27:6379 check backup
范例:对 MySQL 服务实现四层负载
[root@centos7 ~]# vim /etc/haproxy/haproxy.cfg
listen longxuan_mysql
bind 172.31.0.7:3306
mode tcp
balance leastconn
server mysql1 172.31.0.17:3306 check
server mysql2 172.31.0.27:3306 check #如果不写端口号,可以转发,但无法check状态
#或者使用frontend和backend实现
frontend mysql
bind :3306
mode tcp #必须指定tcp模式
default_backend mysqlsrvs
backend mysqlsrvs
mode tcp #必须指定tcp模式
balance leastconn
server mysql1 172.31.0.17:3306
server mysql2 172.31.0.27:3306
[root@centos7 ~]# systemctl restart haproxy
#在后端服务器安装和配置mariadb服务
[root@centos7 ~]# yum -y install mariadb-server
[root@centos7 ~]# mysql -e "grant all on *.* to long@'172.31.0.%' identified by '123456'"
[root@centos7 ~]# vim /etc/my.cnf
[mysqld]
server-id=17 #在另一台主机为27
[root@centos7 ~]# systemctl start mariadb
#测试
[root@centos6 ~]# mysql -ulong -p123456 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| hostname | centos17.longxuan.vip |
+---------------+--------------------------+
[root@centos6 ~]# mysql -ulong -p123456 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| hostname | centos27.longxuan.vip |
+---------------+--------------------------+
[root@centos6 ~]# mysql -ulong -p123456 -h172.31.0.7 -e 'select @@server_id'
+-------------+
| @@server_id |
+-------------+
| 17 |
+-------------+
[root@centos6 ~]# mysql -ulong -p123456 -h172.31.0.7 -e 'select @@server_id'
+-------------+
| @@server_id |
+-------------+
| 27 |
+-------------+
ACL示例-四层访问控制
frontend web_host
bind 172.31.0.7:80
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl static_path path_beg -i /static /images /javascript
acl invalid_src src 192.168.1.0/24 172.31.0.8
###################### acl hosts #################################
use_backend static_path_host if HTTP_1.1 TRUE static_path
tcp-request connection reject if invalid_src #四层ACL控制
default_backend default_web
################### backend hosts ################################
backend php_server_host
mode http
server web1 172.31.0.17 check inter 2000 fall 3 rise 5
backend static_path_host
mode http
server web1 172.31.0.27 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 172.31.0.37:80 check inter 2000 fall 3 rise 5
HAProxy https 实现
haproxy可以实现https的证书安全,从用户到haproxy为https,从haproxy到后端服务器用http通信
但基于性能考虑,生产中证书都是在后端服务器比如nginx上实现
#配置HAProxy支持https协议,支持ssl会话;
bind *:443 ssl crt /PATH/TO/SOME_PEM_FILE
#指令 crt 后证书文件为PEM格式,需要同时包含证书和所有私钥
cat demo.key demo.crt > demo.pem
#把80端口的请求重向定443
bind *:80
redirect scheme https if !{ ssl_fc }
#向后端传递用户请求的协议和端口(frontend或backend)
http_request set-header X-Forwarded-Port %[dst_port]
http_request add-header X-Forwared-Proto https if { ssl_fc }
证书制作
#方法1
[root@centos7 ~]# mkdir /etc/haproxy/certs/
[root@centos7 ~]# cd /etc/haproxy/certs/
[root@centos7 certs]# openssl genrsa -out haproxy.key 2048
[root@centos7 certs]# openssl req -new -x509 -key haproxy.key -out haproxy.crt -subj "/CN=www.longxuan.vip"
#或者用下一条命令实现
[root@centos7 certs]# openssl req -x509 -newkey rsa:2048 -subj "/CN=www.longxuan.vip" -keyout haproxy.key -nodes -days 365 -out haproxy.crt
[root@centos7 certs]# cat haproxy.key haproxy.crt > haproxy.pem
[root@centos7 certs]# openssl x509 -in haproxy.pem -noout -text #查看证书
#方法2
[root@centos7 ~]# mkdir /etc/haproxy/certs/
[root@centos7 ~]# cd /etc/pki/tls/certs
[root@centos7 certs]# make /etc/haproxy/certs/haproxy.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
...
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:longxuan
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:www.longxuan.vip
Email Address []:
[root@centos7 certs]# ll /etc/haproxy/certs/
https配置示例
[root@centos7 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend longxuan_http_port
bind 172.31.0.7:80
###################### https setting ##############################
bind 172.31.0.7:443 ssl crt /etc/haproxy/certs/haproxy.pem
redirect scheme https if !{ ssl_fc } # 注意{ }内的空格
http-request set-header X-forwarded-Port %[dst_port]
http-request add-header X-forwarded-Proto https if { ssl_fc }
mode http
balance roundrobin
log global
option httplog
###################### acl setting ###############################
acl mobile_domain hdr_dom(host) -i mobile.magedu.org
###################### acl hosts #################################
default_backend pc_hosts
################### backend hosts #################################
backend mobile_hosts
mode http
server web1 172.31.0.17:80 check inter 2000 fall 3 rise 5
backend pc_hosts
mode http
#http-request set-header X-forwarded-Port %[dst_port] 也可加在此处
#http-request add-header X-forwarded-Proto https if { ssl_fc }
server web2 172.31.0.27:80 check inter 2000 fall 3 rise 5
[root@centos7 ~]# ss -ntl
修改后端服务器的日志格式
[root@centos27 ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{XForwarded-Port}i\" \"%{X-Forwarded-Proto}i\"" combined
验证https
[root@centos6 ~]# curl -IkL http://www.longxuan.vip
HTTP/1.1 302 Found
content-length: 0
location: https://www.longxuan.vip/
cache-control: no-cache
HTTP/1.1 200 OK
date: Sat, 04 Apr 2021 02:31:31 GMT
[root@centos6 ~]# curl -Ik https://www.longxuan.vip
HTTP/1.1 200 OK
date: Sat, 04 Apr 2021 02:31:50 GMT
#查看后端服务器的访问日志
[root@centos27 ~]# tail /var/log/httpd/access_log
172.31.0.7 - - [04/Apr/2021:10:40:17 +0800] "HEAD / HTTP/1.1" 200 - "-"
"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3libidn/1.18 libssh2/1.4.2" "443" "https"

浙公网安备 33010602011771号