如何自定义Varnish服务器触发Guru Meditation错误时的用户提示信息
Varnish 自定义错误页面及日志编译报错解决方案
1. 修复log.info编译报错
VCL 没有原生的log.info方法,日志输出需要调用标准库提供的能力,操作步骤如下:
- 在
default.vcl文件最开头添加模块导入语句:import std; - 将你写的
log.info(client.ip)替换为合法的日志输出语句:std.log("Request error from client IP: " + client.ip);
2. 自定义Guru Meditation错误页面
Varnish 4.0及以上版本通过vcl_synth子例程处理内置错误页面,后端返回的异常则通过vcl_backend_error处理,完整配置示例如下:
import std; # 处理客户端请求触发的Varnish内置错误 sub vcl_synth { # 打印错误请求的客户端IP到日志 std.log("Error request client IP: " + client.ip); set resp.http.Content-Type = "text/html; charset=utf-8"; # 自定义页面内容,可根据需求调整样式和文案 synthetic({" <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>请求异常</title> <style> .container { max-width: 600px; margin: 100px auto; text-align: center; } h1 { color: #e53935; } </style> </head> <body> <div class="container"> <h1>抱歉,当前请求无法处理</h1> <p>错误码:"} + resp.status + {"</p> <p>请刷新重试或联系站点管理员</p> </div> </body> </html> "}); return (deliver); } # 处理后端服务返回的异常 sub vcl_backend_error { set beresp.http.Content-Type = "text/html; charset=utf-8"; synthetic({" <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>服务异常</title> <style> .container { max-width: 600px; margin: 100px auto; text-align: center; } h1 { color: #e53935; } </style> </head> <body> <div class="container"> <h1>抱歉,后端服务暂时不可用</h1> <p>错误码:"} + beresp.status + {"</p> <p>请稍后再试</p> </div> </body> </html> "}); return (deliver); }
3. 配置验证&服务重启
修改配置后先执行语法检查命令,确认无报错后再重启服务:
- 语法检查:
varnishd -C -f /etc/varnish/default.vcl - 重启服务(根据你系统的服务管理工具选择对应命令):
- Systemd系统:
systemctl restart varnish - SysVinit系统:
service varnish restart
- Systemd系统:
内容的提问来源于stack exchange,提问作者TekDood
相关产品推荐
相关产品推荐

