Varnish Cache 运维教程 / 第02章:安装与初始配置
第02章:安装与初始配置
2.1 安装前准备
2.1.1 系统要求
| 资源 |
最低要求 |
推荐配置 |
| CPU |
2 核 |
8 核及以上 |
| 内存 |
1 GB |
16 GB 及以上(缓存存储用) |
| 磁盘 |
1 GB 可用空间 |
SSD(日志存储) |
| 操作系统 |
Linux(主流发行版) |
Ubuntu 22.04 / Debian 12 / RHEL 9 |
| 架构 |
x86_64 |
x86_64 / ARM64 |
2.1.2 端口规划
在安装前,规划好端口使用:
| 端口 |
用途 |
说明 |
| 6081 |
HTTP 代理端口 |
客户端访问端口(默认) |
| 6082 |
管理端口 |
varnishadm 管理接口 |
| 80 |
标准 HTTP 端口 |
生产环境通常通过 iptables 转发 |
| 443 |
HTTPS 端口 |
由 Hitch 或 Nginx 监听 |
2.2 Ubuntu / Debian 安装
2.2.1 使用官方仓库安装
# 更新系统包
sudo apt-get update
sudo apt-get install -y apt-transport-https curl gnupg
# 添加 Varnish 官方 GPG 密钥
curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | \
sudo gpg --dearmor -o /usr/share/keyrings/varnish-archive-keyring.gpg
# 添加 Varnish 7.x 仓库(以 Ubuntu 22.04 Jammy 为例)
echo "deb [signed-by=/usr/share/keyrings/varnish-archive-keyring.gpg] \
https://packagecloud.io/varnishcache/varnish70/ubuntu/ jammy main" | \
sudo tee /etc/apt/sources.list.d/varnish.list
# 更新并安装
sudo apt-get update
sudo apt-get install -y varnish
# 验证安装
varnishd -V
2.2.2 使用系统默认仓库
# Ubuntu/Debian 默认仓库通常包含较旧但稳定的版本
sudo apt-get update
sudo apt-get install -y varnish
# 检查版本
varnishd -V
2.2.3 安装相关工具
# 安装 TLS 终结代理(推荐)
sudo apt-get install -y hitch
# 安装日志轮转
sudo apt-get install -y logrotate
2.3 RHEL / CentOS / Fedora 安装
2.3.1 使用官方仓库
# 添加 Varnish 7.x 仓库(以 RHEL 9 为例)
sudo cat > /etc/yum.repos.d/varnish.repo << 'EOF'
[varnish70]
name=Varnish Cache 7.0 for Enterprise Linux 9
baseurl=https://packagecloud.io/varnishcache/varnish70/el/9/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish70/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
EOF
# 安装 Varnish
sudo dnf install -y varnish
# 验证安装
varnishd -V
2.3.2 使用 EPEL 仓库
# 安装 EPEL 仓库
sudo dnf install -y epel-release
# 安装 Varnish
sudo dnf install -y varnish
# 验证
varnishd -V
2.4 源码编译安装
2.4.1 安装编译依赖
# Ubuntu/Debian
sudo apt-get install -y \
build-essential \
libpcre3-dev \
libedit-dev \
libncurses-dev \
libjemalloc-dev \
python3 \
python3-sphinx \
automake \
autoconf \
libtool \
pkg-config \
git
# RHEL/CentOS
sudo dnf install -y \
gcc \
make \
pcre-devel \
libedit-devel \
ncurses-devel \
jemalloc-devel \
python3 \
python3-sphinx \
automake \
autoconf \
libtool \
pkgconfig \
git
2.4.2 编译安装
# 克隆源码
git clone https://github.com/varnishcache/varnish-cache.git
cd varnish-cache
# 查看可用标签并切换到稳定版
git tag -l | grep "varnish-7"
git checkout varnish-7.5.1
# 生成配置脚本
./autogen.sh
# 配置编译选项
./configure \
--prefix=/usr/local/varnish \
--enable-jemalloc \
--with-jemalloc=/usr/lib/x86_64-linux-gnu/libjemalloc.so
# 编译(使用多核加速)
make -j$(nproc)
# 安装
sudo make install
# 验证
/usr/local/varnish/sbin/varnishd -V
2.4.3 创建系统服务
# 创建软链接
sudo ln -s /usr/local/varnish/sbin/varnishd /usr/local/bin/varnishd
sudo ln -s /usr/local/varnish/bin/* /usr/local/bin/
# 创建 varnish 用户
sudo useradd -r -s /sbin/nologin varnish
# 创建 systemd 服务文件
sudo tee /etc/systemd/system/varnish.service << 'EOF'
[Unit]
Description=Varnish HTTP Accelerator
Documentation=https://varnish-cache.org/docs/
After=network-online.target
[Service]
Type=forking
User=varnish
Group=varnish
LimitNOFILE=131072
LimitMEMLOCK=85983232
ExecStartPre=/usr/local/bin/varnishd -C -f /etc/varnish/default.vcl
ExecStart=/usr/local/bin/varnishd \
-a :6081 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-s malloc,256m \
-p thread_pool_min=5 \
-p thread_pool_max=500 \
-p thread_pool_timeout=120
ExecReload=/usr/local/bin/varnishadm vcl.load reload /etc/varnish/default.vcl
ExecReload=/usr/local/bin/varnishadm vcl.use reload
ExecStop=/usr/local/bin/varnishadm stop
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
2.5 初始配置
2.5.1 默认配置文件
Varnish 的主配置文件位于 /etc/varnish/default.vcl:
# 备份原始配置
sudo cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak
# 查看默认配置
cat /etc/varnish/default.vcl
2.5.2 最小化配置
创建一个最基础可用的 VCL 配置:
# /etc/varnish/default.vcl - 最小化配置
vcl 4.1;
# 后端服务器定义
backend default {
.host = "127.0.0.1";
.port = "8080";
}
# 请求接收处理
sub vcl_recv {
# 默认使用缓存查找
# 不缓存 POST/PUT/DELETE 等方法
if (req.method != "GET" && req.method != "HEAD") {
return (pass);
}
# 不缓存管理后台
if (req.url ~ "^/admin") {
return (pass);
}
return (hash);
}
# 缓存命中处理
sub vcl_hit {
return (deliver);
}
# 缓存未命中处理
sub vcl_miss {
return (fetch);
}
# 后端响应处理
sub vcl_backend_response {
# 设置默认缓存时间
if (beresp.ttl <= 0s) {
set beresp.ttl = 300s;
}
# 启用 Grace 机制
set beresp.grace = 1h;
}
# 响应发送前处理
sub vcl_deliver {
# 添加缓存状态头部
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
}
2.5.3 配置后端服务器
单后端配置
vcl 4.1;
backend web01 {
.host = "192.168.1.10";
.port = "80";
.connect_timeout = 5s;
.first_byte_timeout = 30s;
.between_bytes_timeout = 10s;
.max_connections = 300;
.probe = {
.url = "/health";
.timeout = 3s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
多后端配置
vcl 4.1;
backend web01 {
.host = "192.168.1.10";
.port = "80";
.probe = {
.url = "/health";
.timeout = 3s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
backend web02 {
.host = "192.168.1.11";
.port = "80";
.probe = {
.url = "/health";
.timeout = 3s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
sub vcl_recv {
# 根据 URL 选择后端
if (req.url ~ "^/api/") {
set req.backend_hint = web02;
} else {
set req.backend_hint = web01;
}
}
2.6 启动参数详解
2.6.1 varnishd 命令行参数
varnishd \
-a :6081 \ # 监听地址和端口
-T localhost:6082 \ # 管理接口地址和端口
-f /etc/varnish/default.vcl \ # VCL 配置文件
-s malloc,256m \ # 存储引擎和大小
-p thread_pool_min=5 \ # 最小线程数
-p thread_pool_max=500 \ # 最大线程数
-p thread_pool_timeout=120 \ # 线程超时时间
-n /var/lib/varnish \ # 工作目录
-P /var/run/varnishd.pid # PID 文件
2.6.2 关键启动参数
| 参数 |
说明 |
默认值 |
-a address:port |
HTTP 监听地址 |
:6081 |
-T address:port |
管理接口地址 |
localhost:6082 |
-f file |
VCL 配置文件路径 |
无 |
-s kind,size |
存储引擎配置 |
malloc,256m |
-n dir |
工作目录 |
/var/lib/varnish/[hostname] |
-p param=value |
设置运行参数 |
无 |
-P file |
PID 文件路径 |
无 |
-j jail |
沙箱机制 |
unix,user=varnish |
-l bytes |
共享内存日志大小 |
80m |
-w min,max |
线程池大小(旧语法) |
无 |
2.6.3 重要运行参数
# 查看当前所有参数
varnishadm param.show
# 查看特定参数
varnishadm param.show thread_pool_min
# 设置参数(临时,重启失效)
varnishadm param.set thread_pool_min 10
# 在启动时设置参数
varnishd -p thread_pool_min=10 -p thread_pool_max=500
线程相关参数
| 参数 |
说明 |
推荐值 |
thread_pool_min |
每个线程池最小线程数 |
5-10 |
thread_pool_max |
每个线程池最大线程数 |
500 |
thread_pool_timeout |
空闲线程回收超时 |
120s |
thread_pools |
线程池数量 |
2(通常等于 CPU 核数的一半) |
thread_queue_limit |
线程队列限制 |
20 |
缓存相关参数
| 参数 |
说明 |
默认值 |
default_ttl |
默认 TTL |
120s |
max_restarts |
最大重试次数 |
4 |
max_retries |
最大后端重试 |
4 |
workspace_client |
客户端工作空间 |
64k |
workspace_backend |
后端工作空间 |
64k |
2.6.4 存储引擎配置
# 内存存储(推荐)
-s malloc,256m # 256MB 内存
-s malloc,1G # 1GB 内存
# 文件存储
-s file,/var/lib/varnish/varnish_storage.bin,1G
# 使用持久存储(实验性)
-s stevedore_name=/path/to/storage,1G
2.7 启动与管理
2.7.1 systemd 服务管理
# 启动 Varnish
sudo systemctl start varnish
# 停止 Varnish
sudo systemctl stop varnish
# 重启 Varnish
sudo systemctl restart varnish
# 重载配置(不中断服务)
sudo systemctl reload varnish
# 查看状态
sudo systemctl status varnish
# 设置开机启动
sudo systemctl enable varnish
# 查看日志
sudo journalctl -u varnish -f
2.7.2 验证安装
# 检查 Varnish 进程
ps aux | grep varnishd
# 检查端口监听
ss -tlnp | grep 6081
# 发送测试请求
curl -I http://localhost:6081/
# 查看缓存状态
curl -sI http://localhost:6081/ | grep -i "x-cache"
# 使用 varnishadm 连接管理接口
varnishadm
2.7.3 常见问题
端口被占用
# 查找占用 6081 端口的进程
sudo lsof -i :6081
sudo fuser -k 6081/tcp
# 或者更改 Varnish 监听端口
# 修改 /etc/default/varnish(Debian)或 /etc/varnish/varnish.params(RHEL)
权限问题
# 确保 Varnish 用户有权访问配置文件
sudo chown -R varnish:varnish /etc/varnish/
sudo chown -R varnish:varnish /var/lib/varnish/
# 检查 SELinux 策略(RHEL/CentOS)
sudo setsebool -P httpd_can_network_connect on
配置语法错误
# 在启动前验证 VCL 配置
sudo varnishd -C -f /etc/varnish/default.vcl
# 通过 varnishadm 测试新配置
varnishadm vcl.load test /etc/varnish/default.vcl
varnishadm vcl.use test
2.8 防火墙配置
2.8.1 iptables
# 允许 Varnish HTTP 端口
sudo iptables -A INPUT -p tcp --dport 6081 -j ACCEPT
# 允许管理端口(仅限本机)
sudo iptables -A INPUT -p tcp --dport 6082 -s 127.0.0.1 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 6082 -j DROP
# 生产环境:将 80 端口转发到 6081
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 6081
2.8.2 firewalld(RHEL/CentOS)
# 允许 Varnish 端口
sudo firewall-cmd --permanent --add-port=6081/tcp
sudo firewall-cmd --reload
2.8.3 ufw(Ubuntu)
# 允许 Varnish 端口
sudo ufw allow 6081/tcp
sudo ufw reload
2.9 生产环境部署清单
# 部署检查清单
# [ ] 系统更新完成
# [ ] Varnish 版本确认
# [ ] 后端服务器配置正确
# [ ] VCL 配置语法验证通过
# [ ] 端口规划确认
# [ ] 防火墙规则配置
# [ ] TLS 代理(Hitch/Nginx)配置
# [ ] 日志轮转配置
# [ ] 监控脚本部署
# [ ] 系统服务设置开机启动
# [ ] 压力测试通过
# [ ] 缓存预热计划准备
2.10 注意事项
重要
- 生产环境中不要将管理端口(6082)暴露给外部网络
- 存储大小应根据可用内存合理设置,预留系统所需内存
- 建议使用
-j unix,user=varnish 启用沙箱安全机制
- 配置变更后建议先使用
varnishd -C -f 验证语法再重启
- 日志共享内存大小(
-l 参数)根据日志量调整
2.11 扩展阅读