Varnish缓存未将Content-Length标头发送到后端服务器

问题描述 投票:0回答:1

我正在尝试使用Varnish缓存来缓存POST请求。我正在使用vcl文件作为

vcl 4.0;

import std;
import bodyaccess;

backend default {
  .host = "**.***.**.***";          
  .port = "**";     
}

sub vcl_recv {
    unset req.http.x-cache; 
    unset req.http.X-Body-Len;

    if (req.method == "POST") {
        std.log("Will cache POST for: " + req.http.host + req.url);
        std.cache_req_body(3000KB);
        set req.http.X-Body-Len = bodyaccess.len_req_body();
        if (req.http.X-Body-Len == "-1") {
            return(synth(400, "The request body size exceeds the limit"));
        }
        return (hash);
    }
}

sub vcl_hash {
    # To cache POST and PUT requests
    if (req.http.X-Body-Len) {
        bodyaccess.hash_req_body();
    } else {
        hash_data("");
    }
}

sub vcl_backend_fetch {
    if (bereq.http.X-Body-Len) {
        set bereq.method = "POST";
    }
}

sub vcl_backend_response {
  set beresp.ttl = 86400s;
  unset beresp.http.Cache-Control;
  set beresp.http.Cache-Control = "public, max-age=86400";
}

sub vcl_hit {
    set req.http.x-cache = "hit";
}

sub vcl_miss {
    set req.http.x-cache = "miss";
}

sub vcl_pass {
    set req.http.x-cache = "pass";
}

sub vcl_pipe {
    set req.http.x-cache = "pipe uncacheable";
}

sub vcl_synth {
    set resp.http.x-cache = "synth synth";
}

sub vcl_deliver {
    if (obj.uncacheable) {
        set req.http.x-cache = req.http.x-cache + " uncacheable" ;
    } else {
        set req.http.x-cache = req.http.x-cache + " cached" ;
    }
    set resp.http.x-cache = req.http.x-cache;
}

当我直接执行发布请求时,它工作正常,但是一旦我通过缓存服务器,后端服务器就会返回“ HTTP错误411。请求必须分块或具有内容长度。”关于调试的进一步]

我从varnishlog中发现的是

         3 BereqMethod    b POST
         3 BereqURL       b /someurl
         3 BereqProtocol  b HTTP/1.1
         3 BereqHeader    b Content-Type: application/json
         3 BereqHeader    b Accept-Language: en-US
         3 BereqHeader    b User-Agent: PostmanRuntime/7.24.0
         3 BereqHeader    b Accept: */*
         3 BereqHeader    b Postman-Token: 6fe71b81-841d-400f-957a-45b6518bfca6
         3 BereqHeader    b Host: *.*.*.*
         3 BereqHeader    b X-Forwarded-For: *.*.*.*
         3 BereqHeader    b X-Body-Len: 161
         3 BereqHeader    b Accept-Encoding: gzip
         3 BereqHeader    b x-cache: miss
         3 BereqMethod    b GET
         3 BereqHeader    b X-Varnish: 3

Varnish没有在后端服务器需要的地方发送Content-Length

如何设置此内容长度

varnish varnish-vcl varnish-4
1个回答
0
投票

您可以将其设置为任何其他标头,即在vcl_recv中:

set req.http.Content-Length = "value" 
© www.soinside.com 2019 - 2024. All rights reserved.