心若寒江雪 发表于 2013-06-25 11:40

【待解决】varnish + Nginx做透明代理不支持https

本帖最后由 心若寒江雪 于 2013-06-25 11:41 编辑

如题,我使用varnish + Nginx做了一个透明代理,但是当我访问https网站的时候就报400的错误,请大侠指教:
nginx.conf
server {
    listen 8080 default_server;
    server_name _;

    access_log/data/nginx/logs/access.logaccess;
    location ~* \.(gif|png|jpg|jpeg|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)$ {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_store /data/nginx/cache$uri;
      proxy_store_access user:rw group:rw all:r;
      proxy_pass $scheme://$host;
      add_header request_uri $request_uri;
    }
    location / {
      proxy_pass $scheme://$host;
      include fastcgi.conf;
    }
}varnish: default.vclbackend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_deliver {
      set resp.http.yougou-hits = obj.hits ;
      if (obj.hits > 0) {
            set resp.http.yougou-varnish = "HIT";
      } else {
            set resp.http.yougou-varsish= "MISS";
      }
}

sub vcl_recv {
    if (req.restarts == 0) {
      if (req.http.x-forwarded-for) {
          set req.http.X-Forwarded-For =
            req.http.X-Forwarded-For + ", " + client.ip;
      } else {
          set req.http.X-Forwarded-For = client.ip;
      }
    }
    if (req.request != "GET" &&
      req.request != "HEAD" &&
      req.request != "PUT" &&
      req.request != "POST" &&
      req.request != "TRACE" &&
      req.request != "OPTIONS" &&
      req.request != "DELETE") {
      /* Non-RFC2616 or CONNECT which is weird. */
      return (pipe);
    }
    if (req.request != "GET" && req.request != "HEAD") {
      /* We only deal with GET and HEAD by default */
      return (pass);
    }
    if (req.http.Authorization || req.http.Cookie) {
      /* Not cacheable by default */
      return (pass);
    }
    return (pass);
}当我访问 https://www.google.com.hk的时候127.0.0.1 - - "CONNECT www.google.com.hk:443 HTTP/1.1" 400 172 "-" "-" -http8080
127.0.0.1 - - "CONNECT www.google.com.hk:443 HTTP/1.1" 400 172 "-" "-" -http8080找到的一个连接(http://www.reistlin.com/2011/04/page/1/)里面说:
“因为 Nginx 不支持 CONNECT,所以无法正向代理 Https 网站(网上银行,Gmail)”
但是我不怎么理解,请大侠指教

心若寒江雪 发表于 2013-06-25 16:29

@各种版主:lol:lol

linsie 发表于 2013-06-25 18:17

:lol帮顶:lol:lol:lol:lol:lol:lol:lol:lol:lol:lol:lol:lol:lol

xf_aj 发表于 2013-11-23 10:54

出现400错误是因为varnish认为客户端请求header行数及长度过大,其默认最大接受的请求header行数为64,最大长度(所有请求header行长度之和)为2048,而https的请求头超过了默认长度从而导致400错误,解决这个问题比较简单,在varnish启动参数中加入:

-p http_max_hdr=256

-p http_req_hdr_len=8192

再重启varnish, 即可解决其400错误。
页: [1]
查看完整版本: 【待解决】varnish + Nginx做透明代理不支持https