0

We're using Varnish 3.0.3. Varnish is behind a load balancer.

We would like to bypass the Varnish cache for a particular IP address. After doing research, I found the following. Unfortunately, it is not working.

    acl passem { "7x.xxx.xxx.xxx"; }
    sub vcl_recv {
    if (!(client.ip ~ passem)) {
    return (pass);
            }
    }

This appears in varnishlog "6 VCL_acl c NO_MATCH passem"

I'm not sure what is wrong. The only thing I can think of is Varnish is not seeing the incoming IP address. This is what I see in varnishlog.

    6 RxHeader     c X-Real-IP: "7x.xxx.xxx.xxx"
    6 RxHeader     c X-Forwarded-For: "7x.xxx.xxx.xxx"

    6 SessionOpen  c 10.10.10.4 58143 0.0.0.0:80
    6 ReqStart     c 10.10.10.4 58143 1026834560

The RxHeader is receiving the correct IP and matches the acl passem, but I don't know if acl passemis instead referencing the SessionOpen IP address, which is the IP address of the load balancer.

4

3 回答 3

4

在 Varnish 中,"X-Real-IP"and"http.x-forwarded-for"是字符串,并且"client.ip"是一个对象。

需要额外的代码将 IP 地址从"X-Forwarded-For"标头复制到 Varnish 的 client_ip 结构中。

以下是使其工作所需的内容。这成功了。归功于 http://zcentric.com/2012/03/16/varnish-acl-with-x-forwarded-for-header/

    C{
    #include <netinet/in.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    }C
    acl passem { "7x.xxx.xxx.xxx"; }
    sub vcl_recv {
    C{
    struct sockaddr_storage *client_ip_ss = VRT_r_client_ip(sp);
    struct sockaddr_in *client_ip_si = (struct sockaddr_in *) client_ip_ss;
    struct in_addr *client_ip_ia = &(client_ip_si->sin_addr);
    char *xff_ip = VRT_GetHdr(sp, HDR_REQ, "\020X-Forwarded-For:");

    if (xff_ip != NULL) {
    inet_pton(AF_INET, xff_ip, client_ip_ia);
    }
    }C
    if (!(client.ip ~ passem)) {
    return (pass);
            }
    }
于 2013-09-06T20:15:20.760 回答
1

是的,您的 client.ip 将是真正的 IP,而不是在标头中转发的任何内容。相反,您需要使用正确的标题req.http.X-Real-IP

于 2013-09-06T06:27:36.953 回答
0

在当前的清漆缓存版本中,您可以使用std.ip(),例如

import std;

sub vcl_recv {
  if (std.ip(req.http.X-Real-IP) !~ passem) {
    return (pass);
  }
}
于 2022-02-24T12:04:23.257 回答