0

我使用 Nginx 作为反向代理并缓存来自上游服务器的代理响应。

因此,在发送给我的客户端之前,我需要在这个缓存文件中添加一个 Content-Length 标头。

我尝试将$upstream_response_length变量添加到 location 指令,但它不起作用。

location /files/ {

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_buffering on;
    chunked_transfer_encoding off;

    ##PROXY CACHE CONFIG

    proxy_cache my_cache;
    add_header X-Proxy-Cache $upstream_cache_status;
    proxy_ignore_headers Cache-Control;
    proxy_cache_valid any 30m;
    add_header Content-Length $upstream_response_length;

    proxy_pass http://upstream_server;

}

我怎样才能做到这一点?

4

1 回答 1

0

如此处所述:

如果处理后的结果长度未知,nginx 会删除 Content-Length 标头(并尽可能使用分块)。例如,如果您使用 gzip、ssi、sub 或附加模块,就会发生这种情况。

因此,您很可能正在使用其中之一。

我相信NGINX“知道”缓存页面的内容长度,所以它应该Content-Length默认发送它们,除非你使用这些模块。

于 2019-09-17T09:31:59.780 回答