当前位置:网站首页>基于HAProxy实现网页动静分离
基于HAProxy实现网页动静分离
2022-06-23 03:38:00 【51CTO】
HAProxy可以通过ACL(Access Control Lists,访问控制列表)来实现动静分离,ACL是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃等。
此次实验笔者将测试环节放在Windows浏览器上,并准备三台主机,一台作为HAProxy服务器,绑定域名www.johnnyfang.com,取名haproxy;两台作为后端web服务器,分别取名static-server和dynamic-server。
- 后端服务器准备
static-server作为静态资源服务器,安装并启动Apache服务。dynamic-server作为动态资源服务器,除了Apache服务,还需安装php和php-fpm。为了检验测试效果,笔者在两台服务器的网站根目录下均创建8个同名同后缀文件以检测效果,其中static-server除了php文件都是图片内容,dynamic-server除了php文件显示状态页,其他都只显示自己的ip。




HAProxy可以通过文件后缀和访问路径等方式实现动静分离。
- 基于文件后缀实现动静分离
2.1 制定ACL规则
HAProxy在使用ACL进行匹配时,可以需要使用frontend+backend的配置方式,同时建议加入注释以便进行更直观的查看。例如我们可以加入注释将ACL列表划分为4栏,首栏是HAProxy服务器的监听端口,第二栏为ACL规则的设置,第三栏为匹配规则判定成立后所需调度的后端服务器分组,最后一栏为后端服务器分组中的具体主机。

ACL列表说明:
①ACL SETTING部分:ACL要以acl开头,acl_static和acl_dynamic为ACL名称,path_end表示匹配规则的判定条件是以uri文件后缀为准,-i表示不区分大小写,.jpg、.js和.php等则是不同ACL成立所对应的文件后缀。
②ACL HOSTS部分:如果上一步的操作中判定acl_static这一ACL名称为真则调度到后端的static_hosts分组中,如果上一步操作中判定acl_dynamic为真则调度到后端的acl_dynamic分组中。
③BACKEND HOSTS部分:类似于普通的backend分组。
2.2 客户端测试
客户端通过浏览器访问时,只要后缀不是.php的,显示内容均为图片,即请求均是由static-server来进行响应的。只有访问请求的后缀为.php时才会查看到php测试页,即是由dynamic-server响应的。




- 基于文件路径实现动静分离
3.1 创建文件路径
HAProxy在基于文件路径来实现动静分离上的具体操作与基于文件后缀的方式大体相同,不同的是在进行匹配规范时要用到path_beg,它表示请求的uri开头,如/static、/images、/css等。例如笔者在后端的两台web服务器均创建/static、/images、/javascript 作为静态文件路径,创建/api作为动态文件路径,并在所有的文件路径里创建默认页面显示各自ip。


3.2 制定ACL规则
HAProxy服务器在配置文件中添加基于文件路径的判定规则,即请求的uri开头为/static、/images和/javascript则判定为acl_static,请求的uri开头为/api的判定为acl_dynamic。这里补充一点,如果一个ACL名称同时存在多个匹配规范,这些匹配规范之间为或的关系,只要有一个成立即可。

3.3 客户端测试
此时客户端通过浏览器访问时,只要请求的uri开头不是/api的,均显示static-server的ip,即请求是由static-server来接收并进行响应的。只有访问的uri开头为/api才会显示dynamic-server的ip。




边栏推荐
- The new version of Kali switches the highest account
- MCU model selection for charging point system design
- 折半查找法
- One of the touchdesigner uses - Download and install
- Heavyweight review: strategies for reliable fMRI measurements
- Postman calls relevant interfaces of CLS API 3.0
- Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
- Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
- 数据交易怎样实现
- Detailed discussion on modular architecture design of MCU firmware
猜你喜欢

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
![[greed] leetcode991 Broken Calculator](/img/6e/ce552b55899c6e8d3c37f524f99f82.png)
[greed] leetcode991 Broken Calculator

直接插入排序
![[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation](/img/b0/4ea8069a88ce19ca7dbfa67ac9fcba.png)
[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]

HAProxy的编译安装及全局配置段说明

第一批00后下场求职:不要误读他们的“不一样”

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e

bubble sort
随机推荐
【LeetCode】23. 合并K个升序链表
WordPress modifying fixed links and pseudo statics
[OWT] OWT client native P2P E2E test vs2017 build 3: no test unit comparison, manually generate vs projects
【LeetCode】翻转链表II
Google Earth engine (GEE) - long time series monthly VCI data extraction, analysis and area calculation (Mexico as an example)
Nature: correlation between oscillatory signals and gene expression supporting human episodic memory coding
【LeetCode】23. Merge K ascending linked lists
Postman calls relevant interfaces of CLS API 3.0
Static code block, code block, constructor execution order
Stress testing with locust on rainbow
直接插入排序
Detailed discussion on modular architecture design of MCU firmware
【LeetCode】179. Maximum number
Goframe framework: log configuration management
Encryption related to returnee of national market supervision public service platform
Three ways to export excel from pages
JS array de duplication, removing the same value
How to realize data transaction
Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
The MIUI 13 development version of Xiaomi mobile phone blocks the chrome application and cannot be opened after installation