作为Web服务器清漆

我正在寻找一种方法来configurationVarnish来处理varnish中的HTTP请求。

更具体地说,对于特定的path(比如* / foo / **),我希望Varnish以200个HTTP状态码和空(或固定消息)正文响应,而不是将请求转发到后端服务器。

我同意迈克尔为此目的滥用/使用vcl_error的简短回应,并向您展示我们在以下示例代码中的含义。

滥用vcl_error我在这里使用了HTTP标准之外的错误代码,并针对这种错误实施了特殊处理。

VCL_RECV中的示例:

 sub vcl_recv { ... # respond HTTP 200 to /ping requests if (req.url ~ "^/ping") { error 700; } # return a 301 redirect if (req.url ~ "^/wrong-target") { error 751 "http://www.example.com/correct-target"; } } 

VCL_ERROR中的示例:

 sub vcl_error { # send response "Pong" (HTTP 200) if (obj.status == 700) { set obj.status = 200; set obj.response = "OK"; set obj.http.Content-Type = "text/plain"; synthetic {"Pong"}; return (deliver); } # send empty response (HTTP 204) if (obj.status == 701) { set obj.status = 204; set obj.response = "No Content"; synthetic {""}; return (deliver); } # redirect 301 if (obj.status == 751) { set obj.http.Location = obj.response; set obj.status = 301; set obj.response = "Moved Permanently"; return (deliver); } # redirect 302 if (obj.status == 752) { set obj.http.Location = obj.response; set obj.status = 302; set obj.response = "Found"; return (deliver); } # Fall through to default behavior for all other exceptions } 

对于这个问题的未来读者,我将@ jens-bradler的vcl移植到了Varnish 4.(Serverfault不允许我发布多行代码作为注释,所以我作为另一个答案张贴在这里)

 vcl 4.0; backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { if (req.url ~ "^/ping") { return (synth(700, "Ping")); } if (req.url ~ "^/wrong-target") { return (synth(751, "http://www.example.com/correct-target")); } } sub vcl_synth { set resp.http.Retry-After = "5"; if (resp.status == 700) { set resp.status = 200; set resp.reason = "OK"; set resp.http.Content-Type = "text/plain;"; synthetic( {"Pong"} ); return (deliver); } if (resp.status == 701) { set resp.status = 204; set resp.reason = "No Content"; set resp.http.Content-Type = "text/plain;"; synthetic( {""} ); return (deliver); } if (resp.status == 751) { set resp.http.Location = resp.reason; set resp.status = 301; set resp.reason = "Moved Permanently"; return (deliver); } if (resp.status == 752) { set resp.http.Location = resp.reason; set resp.status = 302; set resp.reason = "Found"; return (deliver); } return (deliver); } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. }