Varnish基本命令 #
一、服务管理命令 #
1.1 systemctl管理 #
bash
# 启动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 systemctl disable varnish
1.2 直接启动 #
bash
# 前台启动(调试模式)
varnishd -d -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -s malloc,256m
# 后台启动
varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -s malloc,256m
# 指定PID文件
varnishd -P /var/run/varnish.pid -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
1.3 停止服务 #
bash
# 优雅停止
varnishadm stop
# 强制停止
sudo pkill varnishd
# 使用PID文件
sudo kill $(cat /var/run/varnish.pid)
二、varnishadm管理命令 #
2.1 连接管理接口 #
bash
# 使用默认配置连接
varnishadm
# 指定地址和端口
varnishadm -T localhost:6082
# 指定密钥文件
varnishadm -T localhost:6082 -S /etc/varnish/secret
# 直接执行命令
varnishadm -T localhost:6082 status
2.2 状态查看 #
bash
# 查看状态
varnishadm status
# 输出示例
Child in state running
# 查看进程信息
varnishadm pid
2.3 VCL管理 #
bash
# 查看VCL列表
varnishadm vcl.list
# 输出示例
name state temperature
boot active 0
reload_123456 available 0
# 加载新VCL
varnishadm vcl.load new_config /etc/varnish/default.vcl
# 使用VCL
varnishadm vcl.use new_config
# 丢弃VCL
varnishadm vcl.discard old_config
# 查看VCL内容
varnishadm vcl.show boot
2.4 后端管理 #
bash
# 查看后端列表
varnishadm backend.list
# 输出示例
name ref probe health
default 1 0/0 healthy
# 查看后端详细信息
varnishadm backend.list -p
# 设置后端状态
varnishadm backend.set_health default sick
varnishadm backend.set_health default healthy
varnishadm backend.set_health default auto
2.5 缓存管理 #
bash
# 清除所有缓存
varnishadm ban "req.url ~ /"
# 清除特定URL缓存
varnishadm ban "req.url ~ ^/images/"
# 清除特定域名缓存
varnishadm ban "req.http.Host == example.com"
# 清除带Cookie的缓存
varnishadm ban "req.http.Cookie ~ session"
# 查看BAN列表
varnishadm ban.list
2.6 参数管理 #
bash
# 查看所有参数
varnishadm param.show
# 查看特定参数
varnishadm param.show thread_pool_max
# 查看参数详细信息
varnishadm param.show -l thread_pool_max
# 修改参数
varnishadm param.set thread_pool_max 1000
# 重置参数
varnishadm param.reset thread_pool_max
2.7 统计信息 #
bash
# 查看统计信息
varnishadm stats
# 查看统计摘要
varnishadm stats -1
# 按模式查看
varnishadm stats -m MAIN.cache_hit
2.8 其他命令 #
bash
# 查看panic信息
varnishadm panic.show
# 清除panic
varnishadm panic.clear
# 查看连接信息
varnishadm connections
# 查看线程信息
varnishadm thread.show
# 查看帮助
varnishadm help
三、日志命令 #
3.1 varnishlog #
bash
# 实时查看日志
varnishlog
# 只看请求日志
varnishlog -g request
# 只看后端日志
varnishlog -g backend
# 过滤特定URL
varnishlog -q "ReqURL ~ ^/api/"
# 过滤特定状态码
varnishlog -q "RespStatus == 500"
# 过滤特定客户端
varnishlog -q "ReqHeader:Host == example.com"
# 输出到文件
varnishlog -w /var/log/varnish/varnish.log
# 从文件读取
varnishlog -r /var/log/varnish/varnish.log
3.2 varnishlog输出格式 #
text
* << Request >> 12345
- Begin req 12344 rxreq
- Timestamp Start: 1648123456.789012 0.000000 0.000000
- Timestamp Req: 1648123456.789012 0.000000 0.000000
- ReqMethod GET
- ReqURL /index.html
- ReqProtocol HTTP/1.1
- ReqHeader Host: example.com
- ReqHeader User-Agent: Mozilla/5.0
- VCL_call RECV
- VCL_return hash
- VCL_call HASH
- VCL_return lookup
- VCL_call MISS
- VCL_return fetch
- Timestamp Fetch: 1648123456.789012 0.001234 0.001234
- RespProtocol HTTP/1.1
- RespStatus 200
- RespReason OK
- RespHeader Content-Type: text/html
- RespHeader Content-Length: 1234
3.3 varnishncsa #
bash
# NCSA格式日志
varnishncsa
# 自定义格式
varnishncsa -F "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\""
# 输出到文件
varnishncsa -w /var/log/varnish/access.log
# 后台运行
varnishncsa -D -w /var/log/varnish/access.log
# 过滤特定请求
varnishncsa -q "RespStatus >= 400"
3.4 日志格式说明 #
| 格式符 | 说明 |
|---|---|
| %h | 客户端IP |
| %l | 远程登录名(通常-) |
| %u | 远程用户 |
| %t | 时间戳 |
| %r | 请求行 |
| %s | 响应状态码 |
| %b | 响应大小 |
| %{Header}i | 请求头 |
| %{Header}o | 响应头 |
| %{X-Cache}o | 自定义头 |
3.5 varnishtop #
bash
# 实时统计
varnishtop
# 统计请求URL
varnishtop -i ReqURL
# 统计请求头
varnishtop -i ReqHeader
# 统计响应状态
varnishtop -i RespStatus
# 统计后端
varnishtop -i Backend
# 过滤显示
varnishtop -i ReqURL -q "ReqMethod == GET"
3.6 varnishhist #
bash
# 延迟直方图
varnishhist
# 只看请求
varnishhist -m ReqURL
# 只看后端响应
varnishhist -m RespStatus
# 自定义桶大小
varnishhist -b 100
输出示例:
text
| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
| | | | | | | | | | | | |
| | | | |###|###|## | | | | | |
| | | | |###|###|## | | | | | |
| | | | |###|###|## | | | | | |
四、统计命令 #
4.1 varnishstat #
bash
# 实时统计
varnishstat
# 一次性输出
varnishstat -1
# JSON格式输出
varnishstat -j
# XML格式输出
varnishstat -x
# 过滤特定统计项
varnishstat -f MAIN.cache_hit -f MAIN.cache_miss
# 查看计数器
varnishstat -l
4.2 关键统计指标 #
| 指标 | 说明 |
|---|---|
| MAIN.cache_hit | 缓存命中次数 |
| MAIN.cache_miss | 缓存未命中次数 |
| MAIN.client_req | 客户端请求总数 |
| MAIN.backend_conn | 后端连接数 |
| MAIN.n_purges | PURGE请求次数 |
| MAIN.n_expired | 过期对象数 |
| MAIN.n_lru_nuked | LRU淘汰数 |
| MAIN.threads | 当前线程数 |
| MAIN.sess_conn | 会话连接数 |
| MAIN.sess_drop | 会话丢弃数 |
4.3 统计脚本示例 #
bash
#!/bin/bash
# cache_hit_rate.sh - 计算缓存命中率
HITS=$(varnishstat -1 -f MAIN.cache_hit | awk '{print $2}')
MISSES=$(varnishstat -1 -f MAIN.cache_miss | awk '{print $2}')
TOTAL=$((HITS + MISSES))
if [ $TOTAL -gt 0 ]; then
RATE=$(echo "scale=2; $HITS * 100 / $TOTAL" | bc)
echo "Cache Hit Rate: ${RATE}%"
echo "Hits: $HITS, Misses: $MISSES"
else
echo "No requests yet"
fi
五、缓存操作命令 #
5.1 PURGE操作 #
bash
# 使用curl发送PURGE
curl -X PURGE http://localhost:6081/index.html
# PURGE特定URL
curl -X PURGE http://localhost:6081/images/logo.png
# PURGE带Host头
curl -X PURGE -H "Host: example.com" http://localhost:6081/page.html
5.2 BAN操作 #
bash
# BAN所有缓存
curl -X BAN http://localhost:6081/
# BAN特定路径
curl -X BAN -H "X-Ban-Url: ^/images/" http://localhost:6081/
# BAN特定内容类型
curl -X BAN -H "X-Ban-Type: text/html" http://localhost:6081/
VCL配置支持:
vcl
sub vcl_recv {
if (req.method == "BAN") {
if (!client.ip ~ purge) {
return (synth(405, "Not allowed."));
}
if (req.http.X-Ban-Url) {
ban("req.url ~ " + req.http.X-Ban-Url);
} else {
ban("req.url ~ " + req.url);
}
return (synth(200, "Banned."));
}
}
5.3 强制刷新 #
bash
# 使用VLC刷新
curl -H "X-Refresh: 1" http://localhost:6081/page.html
VCL配置:
vcl
sub vcl_recv {
if (req.http.X-Refresh) {
set req.http.X-Pass = "1";
return (pass);
}
}
sub vcl_backend_response {
if (bereq.http.X-Pass) {
set beresp.ttl = 0s;
set beresp.uncacheable = true;
}
}
六、调试命令 #
6.1 VCL调试 #
bash
# 编译检查VCL
varnishd -C -f /etc/varnish/default.vcl
# 详细输出
varnishd -C -f /etc/varnish/default.vcl 2>&1 | less
# 调试模式启动
varnishd -d -f /etc/varnish/default.vcl
6.2 请求追踪 #
bash
# 追踪特定请求
varnishlog -q "ReqURL ~ /api/test" -g request
# 查看请求处理时间
varnishlog -q "ReqURL ~ /api/test" -i Timestamp
# 查看VCL调用
varnishlog -q "ReqURL ~ /api/test" -i VCL_call -i VCL_return
6.3 后端调试 #
bash
# 测试后端连接
curl -H "Host: example.com" http://backend-server:8080/test
# 查看后端健康状态
varnishadm backend.list -p
# 追踪后端请求
varnishlog -g backend -q "BackendOpen"
七、监控命令 #
7.1 实时监控脚本 #
bash
#!/bin/bash
# varnish_monitor.sh
while true; do
clear
echo "=== Varnish Monitor ==="
echo "Time: $(date)"
echo ""
# 缓存命中率
HITS=$(varnishstat -1 -f MAIN.cache_hit | awk '{print $2}')
MISSES=$(varnishstat -1 -f MAIN.cache_miss | awk '{print $2}')
TOTAL=$((HITS + MISSES))
if [ $TOTAL -gt 0 ]; then
RATE=$(echo "scale=2; $HITS * 100 / $TOTAL" | bc)
echo "Cache Hit Rate: ${RATE}%"
fi
echo ""
# 连接数
echo "Connections:"
varnishstat -1 -f MAIN.sess_conn -f MAIN.sess_drop
echo ""
# 后端状态
echo "Backend Status:"
varnishadm backend.list
echo ""
# 线程数
echo "Threads:"
varnishstat -1 -f MAIN.threads -f MAIN.threads_limited
echo ""
sleep 2
done
7.2 健康检查 #
bash
#!/bin/bash
# health_check.sh
# 检查Varnish进程
if ! pgrep -x varnishd > /dev/null; then
echo "CRITICAL: Varnish process not running"
exit 2
fi
# 检查端口
if ! nc -z localhost 6081; then
echo "CRITICAL: Port 6081 not responding"
exit 2
fi
# 检查管理接口
if ! varnishadm status > /dev/null 2>&1; then
echo "WARNING: Management interface not responding"
exit 1
fi
# 检查缓存命中率
HITS=$(varnishstat -1 -f MAIN.cache_hit | awk '{print $2}')
MISSES=$(varnishstat -1 -f MAIN.cache_miss | awk '{print $2}')
TOTAL=$((HITS + MISSES))
if [ $TOTAL -gt 100 ]; then
RATE=$(echo "scale=2; $HITS * 100 / $TOTAL" | bc)
if (( $(echo "$RATE < 50" | bc -l) )); then
echo "WARNING: Cache hit rate is low: ${RATE}%"
exit 1
fi
fi
echo "OK: Varnish is healthy"
exit 0
八、性能分析命令 #
8.1 内存分析 #
bash
# 查看内存使用
varnishstat -1 -f MAIN.s0.g_bytes -f MAIN.s0.g_space
# 查看对象数
varnishstat -1 -f MAIN.n_object -f MAIN.n_vampireobject
# 查看LRU活动
varnishstat -1 -f MAIN.n_lru_nuked -f MAIN.n_lru_moved
8.2 线程分析 #
bash
# 查看线程统计
varnishstat -1 -f MAIN.threads -f MAIN.threads_limited -f MAIN.threads_created
# 查看线程队列
varnishstat -1 -f MAIN.thread_queue_len
# 查看会话统计
varnishstat -1 -f MAIN.sess_queued -f MAIN.sess_dropped
8.3 性能瓶颈分析 #
bash
# 查看后端响应时间
varnishlog -i Timestamp -i BereqURL | grep -A1 "BereqURL" | grep "Beresp"
# 查看慢请求
varnishlog -q "Timestamp:Process[2] > 1.0"
# 查看失败请求
varnishlog -q "RespStatus >= 500"
九、日志管理 #
9.1 日志轮转 #
bash
# /etc/logrotate.d/varnish
/var/log/varnish/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0644 vcache vcache
postrotate
systemctl reload varnishncsa > /dev/null 2>&1 || true
endscript
}
9.2 启动日志服务 #
bash
# 启动varnishncsa服务
sudo systemctl start varnishncsa
sudo systemctl enable varnishncsa
# 启动varnishlog服务
sudo systemctl start varnishlog
sudo systemctl enable varnishlog
十、常用命令速查 #
10.1 服务管理 #
| 命令 | 说明 |
|---|---|
| systemctl start varnish | 启动服务 |
| systemctl stop varnish | 停止服务 |
| systemctl restart varnish | 重启服务 |
| systemctl status varnish | 查看状态 |
| varnishadm stop | 优雅停止 |
10.2 配置管理 #
| 命令 | 说明 |
|---|---|
| varnishadm vcl.list | 查看VCL列表 |
| varnishadm vcl.load name file | 加载VCL |
| varnishadm vcl.use name | 使用VCL |
| varnishadm vcl.discard name | 删除VCL |
| varnishreload | 热重载配置 |
10.3 缓存管理 #
| 命令 | 说明 |
|---|---|
| varnishadm ban “req.url ~ /” | 清除所有缓存 |
| curl -X PURGE url | PURGE特定URL |
| varnishadm ban.list | 查看BAN列表 |
10.4 日志查看 #
| 命令 | 说明 |
|---|---|
| varnishlog | 实时日志 |
| varnishncsa | NCSA格式日志 |
| varnishtop | 日志统计 |
| varnishhist | 延迟直方图 |
10.5 统计信息 #
| 命令 | 说明 |
|---|---|
| varnishstat | 实时统计 |
| varnishstat -1 | 一次性输出 |
| varnishadm stats | 统计信息 |
十一、总结 #
本章我们学习了:
- 服务管理:启动、停止、重启、状态查看
- varnishadm:管理接口命令详解
- 日志命令:varnishlog、varnishncsa、varnishtop
- 统计命令:varnishstat统计指标
- 缓存操作:PURGE、BAN操作
- 调试命令:VCL调试、请求追踪
- 监控命令:实时监控、健康检查
- 性能分析:内存、线程、瓶颈分析
掌握基本命令后,让我们进入下一章,深入学习VCL语言!
最后更新:2026-03-28