清漆client.ip说127.0.0.1

所以我有一个像Nginx的设置 – > varnish – > apache2如果我得到一个带有静态文件的请求,它会通过nginx发送到varnish,再次返回给nginx,因为它比apache2服务器快得多。 我的问题是,当我做一个

sub vcl_fetch { set beresp.http.X-Tabulex-Client = client.ip; 

看看客户端的IP地址是127.0.0.1(X-Tabulex-Client 127.0.0.1)在vcl_recv中我有:

sub vcl_recv {

  if((!req.url ~ "^/typo3temp/*" && !req.url ~ "^/typo3/*") && req.url ~ "\.(jpg|css|gif|png|js)(\?.*|)$") { set req.backend = aurum; set client.identity = req.http.X-Forwarded-For; } elseif(client.ip == "192.168.3.189") { /* Traffic from the other Varnish server, serve using real backends */ set req.backend = balance; /* Set client.identity to the X-Forwarded-For (the real IP) */ set client.identity = req.http.X-Forwarded-For; } else { /* Traffic coming from internet, use the other Varnish as backend */ set req.backend = iridium; } 

nginxconfiguration包含

  proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_intercept_errors on; 

当第一次发送清漆,再次从清漆接收时,什么都没有。

我不知道问题在哪里。 我期望client.ip包含外部IP地址,所以我可以使用它的ACL。 有任何想法吗?

client.ip的值是127.0.0.1因为nginx是客户端。 对于Varnish来说,掩盖这个价值是没有意义的 – 即使在像Varnish坐在前端代理之后的情况下,你通常也想根据实际连接到Varnish的IP地址做出决定。

你真正想要做的是让nginx把远程客户端的IP地址放到一个专用的头部(你已经用X-Real-IP做了),并用它来做连接决定。 我们在我们的环境中完成了这个工作,在这个环境中,Apache提供了在varnish SSL连接,然后我们使用这个头来做出访问决定。

它不如使用client.ip (你不能使用acl来匹配它),但它的工作原理。 我们做这样的事情:

 if (! ( req.http.X-Real-IP ~ "^10\." || req.http.X-Real-IP ~ "^100\.100\." || req.http.X-Real-IP ~ "^200\.200\." )) { error 403 "Forbidden"; } 

Varnish没有提供用自定义头部覆盖client.ip的原生机制,但无论如何可以解决这个问题,因为你可以在你的configuration中插入任意的C代码。

下面是一个与您的情况完全相同的示例,其中包括将client.ipreplace为另一个值的示例,以便可以在Varnish ACL中使用它。

如果您在Rackspace Cloud负载平衡器后面使用Varnish作为您的前端Web服务器(我们的后端是NGINX),那么您需要使用类似以下的模式:

 if (!( req.http.X-Forwarded-For ~ "1\.2\.3\.4" || req.http.X-Forwarded-For ~ "2\.3\.4\.5" )) { # IP-based Rules } 

Rackspace Cloud负载平衡器不会将127.0.0.1任何内容作为client.ip传递给Varnish。 另外,我尝试使用更具限制性的模式,例如^2\.3\.4\.5$但它不匹配。 我认为负载均衡器正在为X-Forwarded-For头添加额外的字符。

如果req.http.X-Real-IP在您的请求中可用,那么您可以使用std模块的函数ip()将string(如req.http.X-Real-IP)转换为IPtypes,然后使用〜运算符将其与ACL列表进行比较。 这比用一些IPstring比较多倍时间更有效率。 acl aclRestricted { "1.1.1.1"; "2.2.2.2"; } if (std.ip(req.http.X-Real-IP, "0.0.0.0") ~ aclRestricted ) { ... }

参考清漆V4.1: https : //varnish-cache.org/docs/4.1/reference/vmod_std.generated.html#func-ip