我们在 Plone 的前面使用 Varnish。在 Plone 出现故障或出现内部错误的情况下,我们希望显示一个用户友好的静态 HTML 页面,其中包含一些 CSS 样式 + 图像。(“服务器正在更新页面”)
如何配置 Varnish 来做到这一点?
另一种简单的方法是使用 varnish 附带的 std vmod。这是我的首选方式,因为我喜欢在配置之外显示错误消息,以防您希望对不同的状态代码有多个响应。
import std;
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic std.fileread("/path/to/file.html");
return (deliver);
}
您可以自定义在 vlc_error 上提供的合成页面。default.vcl 配置文件已经通过提供著名的“Guru Meditation”错误页面(啊,那些美好的 Amiga 日子)显示了如何做到这一点。
自定义示例:
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Sorry, server under maintainance - My Website"</title>
<style src="css/style.css"></style>
</head>
<body>
<h1>The server is being updated</h1>
<p>Please check back later. Meanwhile, here's a picture of a rabbit with a pancake on its head:</p>
<img src="img/wabbit.jpg" alt="awwwww!" />
</body>
</html>
"};
return (deliver);
}
目前使用 Varnish 4 并没有太多帮助。
这是我最终得到的结果:
sub vcl_backend_error {
set beresp.http.Content-Type = "text/html; charset=utf-8";
synthetic(std.fileread("/var/www/errors/500.html"));
return (deliver);
}
有关更多信息,请参阅升级到 4.0 文档。
如果您更喜欢从静态文件传递错误页面,您可以使用一些 C 代码覆盖 vcl_error():
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
C{
#include <stdio.h>
#include <string.h>
FILE * pFile;
char content [100];
char page [10240];
char fname [50];
page[0] = '\0';
sprintf(fname, "/var/www/error/index.html", VRT_r_obj_status(sp));
pFile = fopen(fname, "r");
while (fgets(content, 100, pFile)) {
strcat(page, content);
}
fclose(pFile);
VRT_synth_page(sp, 0, page, "<!-- XID: ", VRT_r_req_xid(sp), " -->", vrt_magic_string_end);
return (deliver);
}C
}