公司代理后面的清漆或鱿鱼反向代理

时间:2018-08-07 13:59:44

标签: reverse-proxy varnish squid

如何配置清漆(或者是否有人可以提示我鱿鱼即时)来缓存来自后端的请求,但通过http_proxy连接到后端

所以我尝试:

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

backend corp_proxy {
  .host = "proxy";
  .port = "8080";
}
sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    set req.backend_hint = corp_proxy;
    set req.url ="http://" + req.http.host + req.url;
}

1 个答案:

答案 0 :(得分:2)

清漆(或其他Web缓存代理)基于与缓存相关的标头(例如Cache-Control)来缓存请求。
不幸的是,许多Web应用程序没有正确设置这些标头。因此,我们应该使用更具侵略性的方法来缓存一些众所周知的项目,例如图片,.js.css文件。

此外,此行set req.url ="http://" + req.http.host + req.url;不是必需的,因为Varnish会将请求原样发送到您指定的后端。

这是我推荐的配置:

backend corp_proxy {
  .host = "proxy";
  .port = "8080";
}

sub vcl_recv {
  
  // Determine backend
  if ( req.http.host ~ ".example.com" ) {
    set req.backend_hint = corp_proxy;
    
    // Determine cacheable items
    if( req.url ~ "\.(css|js|jpg|jpeg|png|gif|ico) {
      unset req.http.Cookie;
      unset req.http.Cache-Control;
    }
  }
  
}

sub vcl_backend_response {
  
  if( bereq.http.host ~ ".example.com" ) {
    if (bereq.url ~ "\.(css|jsjpg|jpeg|png|gif|ico)") {
      set beresp.ttl = 20m; // I opt for 20 minutes of caching by your mileage may vary
  }

}