更改清漆 4 503 错误

Ali*_*ade 7 varnish

如何更改清漆 503 错误?
我该如何自定义它?
我正在使用清漆 v 4

现在开始工作了

sub vcl_synth {
    set resp.http.Content-Type = "text/html; charset=utf-8";
    set resp.http.Retry-After = "5";
    synthetic( {"<!DOCTYPE html>
<html>
  <head>
    <title>Under Maintenance</title>
  </head>
  <body>
    <h1>Under Maintenance</h1>
    <p></p>
    <hr>
  </body>
</html>
"} );
    return (deliver);
}
Run Code Online (Sandbox Code Playgroud)

小智 14

我想提出一个替代方案...请在下面找到一个示例 default.vcl 文件

vcl 4.0;

import std;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_backend_response {
       if (beresp.status == 503 && bereq.retries < 5 ) {
       return(retry);
 }
}

sub vcl_backend_error {
      if (beresp.status == 503 && bereq.retries == 5) {
          synthetic(std.fileread("/etc/varnish/error503.html"));
          return(deliver);
       }
 }

sub vcl_synth {
    if (resp.status == 503) {
        synthetic(std.fileread("/etc/varnish/error503.html"));
        return(deliver);
     }
}

sub vcl_deliver {
    if (resp.status == 503) {
        return(restart);
    }
  }
Run Code Online (Sandbox Code Playgroud)

然后你可以在 error503.html 中保存你的自定义 html


qui*_*ver 4

Varnish 4 中有两种错误。一种是后端获取错误。vcl_backend_error处理此类错误。另一个是VCL 中产生的错误。vcl_synth处理此类错误。

就您而言,您正在自定义vcl_error子例程,这不适用于后端错误。

您可以从下面的default.vcl中区分这两种错误。

vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (req.url ~ "^/404") {
        return (synth(999, "make 404 error explicitly"));
    }
}

sub vcl_backend_response {
}

sub vcl_deliver {
}

sub vcl_backend_error {
    set beresp.http.Content-Type = "text/html; charset=utf-8";
    synthetic( {"errors due to backend fetch"} );
    return (deliver);
}

sub vcl_synth {
    if (resp.status == 999) {
        set resp.status = 404;
        set resp.http.Content-Type = "text/plain; charset=utf-8";
        synthetic({"errors due to vcl"});
        return (deliver);
    }
    return (deliver);
}
Run Code Online (Sandbox Code Playgroud)

确认错误信息

$ curl http://localhost:6081/   # If the backend server is not running, "503 Backend fetch failed" error occurs 
errors due to backend fetch
$ curl http://localhost:6081/404/foo
errors due to vcl
Run Code Online (Sandbox Code Playgroud)