精读笔记(RHCA 英文教材)· RH358 Chapter 9 Optimizing Web Server Traffic
精读笔记(RHCA 英文教材)· RH358 Chapter 9 Optimizing Web Server Traffic
教材原文:RHCA 官方英文教材(教材第 21~23 页)(OCR 整书版已从本站移除,本页为章节精读) 说明:本手册第 7/8/9 章均含同款 Structure 页;本笔记按“讲义要点 + 必要补充”双语整理(Varnish/HAProxy 细节参考 RHEL8 官方文档与各包自带文档)。 关联知识:RH358 Ch8(httpd/Nginx 是本章被加速的 Web 后端)、
02-08-Ansible自动化、RH358 Ch1(systemd);本手册方法为 Lecture + Guided Exercise。
Chapter 9 | Optimizing Web Server Traffic(优化 Web 服务器流量)
- 一句话目标:Improve performance of your web servers by using Varnish to cache static content being served and HAProxy to terminate TLS connections and balance load between servers.(用 Varnish 缓存静态内容、用 HAProxy 终结 TLS 并在多台服务器间负载均衡,从而提升 Web 性能)
Chapter Objectives(本章目标,双语)
By the end of this chapter, students should be able to:
- Improve web site performance by caching static web content with Varnish.(用 Varnish 缓存静态内容提升网站性能)
- Improve web site performance by using HAProxy as a load balancer and an HTTPS terminator in front of your Varnish Cache.(用 HAProxy 做负载均衡 + HTTPS 终结器,放在 Varnish Cache 前面提升性能)
- Automate the configuration of HAProxy and Varnish using Ansible.(用 Ansible 自动化 HAProxy 与 Varnish 的配置)
Chapter Schedule(课时表,共 230 分钟)
| Section | 主题 | 方法(P:Lecture / A:Guided Exercise) |
|---|---|---|
| 1 | Caching Static Content with Varnish | P 40 分钟 + A 40 分钟 |
| 2 | Terminating HTTPS Traffic and Load Balancing with HAProxy | P 55 分钟 + A 30 分钟 |
| 3 | Automating Web Service Optimization | P 15 分钟 + A 25 分钟 |
| - | Lab Review(Performance Checklist) | 25 分钟 |
Key Takeaways(本章要点 6 条)
Varnish Cache speeds up web server response by caching commonly accessed objects in memory.(Varnish 把常用对象缓存在内存中,加快 Web 响应) The cache configuration and access control list for Varnish is specified by writing statements in /etc/varnish/default.vcl using the Varnish Control Language (VCL).(Varnish 的缓存规则与访问控制清单用 VCL 语言写在 /etc/varnish/default.vcl) To change the network port used by Varnish, you override the systemd service file's ExecStart parameter.(改 Varnish 监听端口要覆盖 systemd 服务文件的 ExecStart 参数) You can use HAProxy to balance load between multiple web servers.(HAProxy 可在多台 Web 服务器间负载均衡) HAProxy can also terminate HTTPS connections and forward them through a Varnish Cache for a web server.(HAProxy 可终结 HTTPS,再转发给 Varnish Cache 再到 Web 服务器) You can use Ansible and its haproxy module to perform rolling upgrades of web content and software for an HAProxy-based load balanced web farm.(Ansible 的 haproxy 模块可对 HAProxy 负载均衡 Web 场做内容/软件滚动升级) 中文归纳考点:① Varnish 内存缓存静态内容;② VCL 写在 /etc/varnish/default.vcl;③ 端口=覆盖 systemd ExecStart;④ HAProxy 负载均衡;⑤ HAProxy 终结 HTTPS 后转发 Varnish;⑥ haproxy 模块管“已装实例”做滚动更新(非部署)。
Instructor Tips and Suggestions(讲师提示原文要点)
- Varnish 文档包:
varnish-docs在/usr/share/doc/varnish-docs/html/部署一份 Varnish 文档副本(对应 varnish-cache.org/docs),离线可查。 - HAProxy 文档包:
haproxy包自带完整文档于/usr/share/doc/haproxy/;其中configuration.txt描述全部配置参数并带示例,可直接 grep 检索:[user@host haproxy]$ grep "redirect.*ssl" configuration.txt redirect scheme https if !{ ssl_fc } - 架构选型(考点):Varnish 本身也能当负载均衡器(此时可不用 HAProxy);但 Varnish 不能做 HTTPS 终结器(terminator)——需要该功能就必须保留 HAProxy。链路常为:客户端 → HAProxy(终结 TLS + 均衡)→ Varnish(缓存)→ Web 服务器。
- 真实 IP 问题:Varnish 与 HAProxy 串接时,可用 PROXY protocol 让后端拿到客户端真实 IP(讲师给 Varnish 4.1 & HAProxy 博文链接)。
- 自动化口径:没有专用模块“部署”Varnish/HAProxy——讲义与练习用标准模块(yum、service、firewalld、copy);Ansible Galaxy 有社区角色可部署,但 Red Hat 一概不支持;
haproxy模块不是部署用,而是控制已安装实例(滚动升级场景)。
补充精讲 A:Varnish 缓存(Section 1 对应知识)
- 定位:反向缓存代理,把 Web 服务器常被访问的对象(页面/静态资源)缓存在内存,命中即回,大幅降低后端压力与响应延迟。
- 安装与启停:
dnf install -y varnish;systemctl enable --now varnish;默认监听6081/tcp,把 Web 后端配成 origin。 - 核心配置 = VCL(Varnish Control Language):
/etc/varnish/default.vcl里的语句描述缓存规则与访问控制(ACL)。常用子程序概念:vcl_recv(请求进来)、vcl_backend_response(后端响应)、vcl_deliver(返回给客户端);语句如:规则示例:vcl 4.0; backend default { .host = "127.0.0.1"; .port = "8080"; }if (req.url ~ "^/static/") { return (hash); }、unset cookie/return(pass)跳过缓存等——具体语句以学生手册/官方 VCL 参考为准。 - 改监听端口(Key Takeaway 3,考点):不像 httpd 在配置文件里改 Listen;Varnish 端口由 systemd 服务 ExecStart 参数(
-a :port)决定 → 用systemctl edit varnish(或 drop-in override)覆盖ExecStart再 restart:systemctl edit varnish # 添加 [Service] ExecStart=/usr/sbin/varnishd ... -a :80 ... systemctl restart varnish - 放行与验证:
firewall-cmd --add-service=varnish(若对外提供 6081);日志/统计varnishlog、varnishstat、varnishadm查看缓存命中。 - 常见拓扑:Varnish 前置缓存(6081)→ 后端 httpd/Nginx(如 8080)需要 httpd 配置 Listen 改到非 80 并让 Varnish 指向它;或 HAProxy → Varnish → web(Section 2 链路)。
补充精讲 B:HAProxy 负载均衡与 HTTPS 终结(Section 2 对应知识)
- 定位:高可用代理/负载均衡器;Key Takeaways 4、5 双职——在多个 Web 服务器间均衡流量 + **终结 HTTPS(解 TLS)**再转内部明文链路。
- 安装与启停:
dnf install -y haproxy;systemctl enable --now haproxy;配置/etc/haproxy/haproxy.cfg。 - 配置骨架(概念示例,具体以教材/官方文档为准):
global log /dev/log local0 maxconn 4096 defaults mode http timeout connect 5s timeout client 50s timeout server 50s frontend www bind *:80 default_backend web_servers frontend wwws bind *:443 ssl crt /etc/pki/tls/private/www.pem default_backend varnish_servers backend web_servers balance roundrobin server web1 10.0.0.11:8080 check server web2 10.0.0.12:8080 check backend varnish_servers server varnish1 10.0.0.21:6081 check - 要点:
frontend(入口/bind/策略)+backend(服务器组)+server行;balance roundrobin等算法;check做健康检查自动摘除故障节点。- HTTPS 终结:443 前端绑证书(
ssl crt <pem>),解 TLS 后把请求转发给后端(后端链路可明文);这也是为什么说“HAProxy 可终结 HTTPS 而 Varnish 不能”。 - 前端架构:HAProxy → Varnish(缓存层)→ Web(避免每个请求都打到后端;讲师链接讨论 HAProxy 在前还是 Varnish 在前、PROXY protocol 取真实 IP)。
- 配置检查与生效:
haproxy -c -f /etc/haproxy/haproxy.cfg(或haproxy -f ... -c)语法检查 →systemctl reload haproxy。 - 防火墙:对外放行 http/https(80/443)即可,内部端口不必暴露。
- 讲师提供的常见排障/实现片段:
redirect scheme https if !{ ssl_fc }(把明文 80 请求重定向到 HTTPS)。
补充精讲 C:用 Ansible 自动化(Section 3 对应知识)
- 部署自动化(无专用模块,考点):Varnish/HAProxy 没有红帽支持的专用“安装/配置”模块 → 与 RH358 Ch7/Ch8 同一套标准模块组合:handlers 里
# 思路示例(非讲义原文) - name: Install varnish / haproxy dnf: name={{ item }} state=present loop: [varnish, haproxy] - name: Configure template: src: "{{ item.src }}" dest: "{{ item.dest }}" notify: reload proxy - name: Open firewall ports firewalld: port={{ item }} permanent=yes state=enabled loop: [80/tcp, 443/tcp]systemctl reload varnish/haproxy;配置模板按需覆盖 default.vcl / haproxy.cfg。 - 滚动升级(Key Takeaway 6,考点:haproxy 模块的真实用途):
haproxyAnsible 模块不部署 HAProxy,而是控制已运行实例把某台后端 server 置为disabled/enabled(drain/disable/enable),配合service/dnf在零中断下升级 Web 内容或软件:# 思路示例:先摘除节点再升级 - name: Disable backend node in haproxy haproxy: state: disabled backend: web_servers host: web1.example.com - name: Upgrade web software on web1 dnf: name=httpd state=latest - name: Re-enable backend node haproxy: state: enabled backend: web_servers host: web1.example.com - Galaxy 角色(如 geerlingguy.varnish / community)可部署整套,但 Red Hat 不支持任何社区角色——生产自行评估。
命令速查表
| 命令/文件 | 用途 |
|---|---|
dnf install varnish / haproxy | 安装 Varnish / HAProxy |
systemctl enable --now varnish / haproxy | 启动并开机自启 |
/etc/varnish/default.vcl | VCL 缓存规则/ACL(vcl_recv/vcl_backend_response/vcl_deliver) |
systemctl edit varnish → 覆盖 ExecStart -a :port | 修改 Varnish 监听端口(Key Takeaway 3) |
varnishlog / varnishstat / varnishadm | Varnish 日志 / 统计 / 管理 |
/etc/haproxy/haproxy.cfg | HAProxy 配置(frontend/backend/server) |
haproxy -f /etc/haproxy/haproxy.cfg -c | 配置语法检查 |
systemctl reload haproxy | 配置热加载 |
bind *:443 ssl crt <pem> | 前端终结 HTTPS(证书+私钥合一 pem) |
redirect scheme https if !{ ssl_fc } | 明文 80 → HTTPS 跳转(configuration.txt 示例) |
/usr/share/doc/varnish-docs/html/ | varnish-docs 离线文档 |
/usr/share/doc/haproxy/configuration.txt | HAProxy 全参数参考(可 grep) |
| dnf+template+service+firewalld(+handler) | Varnish/HAProxy 部署自动化标准模块组合 |
haproxy: 模块(state=disabled/enabled, backend, host) | 控制已装实例做滚动升级(非部署) |
核心词汇表
| 英文 | 中文速记 |
|---|---|
| Varnish Cache | 内存反向缓存代理(静态内容加速) |
| VCL(Varnish Control Language) | Varnish 规则语言(default.vcl) |
| default.vcl | Varnish 主配置(缓存/ACL 语句) |
| ExecStart override | 改 Varnish 端口的方式(systemd 覆盖) |
| HAProxy | 负载均衡 / 反向代理 / HTTPS 终结器 |
| load balancer | 负载均衡(多后端 roundrobin+check) |
| HTTPS terminator | TLS 终结器(解 SSL 再转后端) |
| frontend / backend / server | HAProxy 配置三段(入口/后端组/成员) |
| ssl crt <pem> | HAProxy 绑定证书(443) |
| PROXY protocol | 代理协议(后端获取客户端真实 IP) |
| varnish-docs | Varnish 离线文档包 |
| configuration.txt | haproxy 包自带全参数文档 |
| rolling upgrade | 滚动升级(haproxy 模块 disable→升级→enable) |
| haproxy(Ansible 模块) | 控制已运行实例(非部署) |
| yum/service/firewalld/copy | 自动化部署标准模块组合 |
| redirect scheme https | http→https 强制跳转片段 |
本章自测
- Varnish 用哪种结构存储缓存、配置写在哪个文件、用什么语言?
- 为什么改 Varnish 端口不是改“配置文件里的 Listen”?正确做法是什么?
- HAProxy 的两个角色是什么?对照链路“客户端 → HAProxy → Varnish → Web”,各层承担什么?
- 讲师为什么说“Varnish 可当负载均衡器但仍可能需要 HAProxy”?关键差别在哪个能力?
redirect scheme https if !{ ssl_fc }实现什么效果?它来自哪份文档?- “部署 Varnish/HAProxy”用什么模块组合?为什么没有专用模块?
haproxyAnsible 模块的定位是什么?滚动升级一个 Web 节点要哪三步?- Galaxy 上的 Varnish/HAProxy 角色能用吗?红帽官方态度是什么?
- 后端要拿客户端真实 IP,在 Varnish↔HAProxy 串接场景应启用什么机制?
- 把 Objectives 三条与 Key Takeaways 六条对应起来;画一条“HTTP/HTTPS 客户端 → 最优缓存+均衡”的完整链路并标注每跳端口。
