首页 > 解决方案 > Varnish 4 + Pounds - 绕过特定 IP 地址的缓存

问题描述

我正在尝试将 Varnish 配置为不对特定 IP 使用缓存。

我已经使用PoundCentos上配置了Varnish 4Apache来管理 HTTPS 请求。

我试图遵循这种方法:

Varnish - 为 IP 地址绕过缓存

基于

https://zcentric.com/2012/03/16/varnish-acl-with-x-forwarded-for-header/

使用一些 C 代码来管理 IP。建议的代码适用于 Varnish3(例如“sp”不再存在,现在有一个 ctx 变量)

我尝试使用这种方法Inline C Varnish (VCL_deliver),但我得到一个 *"initialization from incompatible pointer type [-Werror] in struct sockaddr_storage client_ip_ss = VRT_r_client_ip(ctx);"错误可能是因为类型也已更改。

我尝试使用的代码是:

struct sockaddr_storage *client_ip_ss = VRT_r_client_ip(ctx); 
struct sockaddr_in *client_ip_si = (struct sockaddr_in *) client_ip_ss; 
struct in_addr *client_ip_ia = &(client_ip_si->sin_addr); 
const struct gethdr_s hdr = { HDR_REQ, "20X-Forwarded-For:" }; 
char *xff_ip = VRT_GetHdr(ctx, &hdr);

但我做错了什么。

我现在有点迷茫,如何在 Varnish 4 上禁用特定 IP 的清漆?

谢谢

标签: ccentosvarnishvarnish-4pound

解决方案


请不要在 Varnish 中编写内联 C:这是有风险的,并且您正在寻找的解决方案已经在 Varnish 中自动实现。

请记住,不再支持 Varnish v3 和 v4,请使用 Varnish 6

Varnish 自动设置X-Forwarded-For页眉

如果您想根据该X-Forwarded-For值从缓存中排除项目,您仍然可以使用client.ip该值并将其与acl.

Varnish 将自动从其客户端获取 IP 并将其存储在X-Forwarded-For标头中。这意味着 的值与client.ip完全相同req.http.X-Forwarded-For

多个代理和代理协议

在到达 Varnish 之前使用其他代理时,您必须确保它们通过PROXY 协议进行通信。Varnish 支持 PROXY 协议,你的其他代理也应该支持。

在你的情况下,它是英镑。Varnish 社区建议Hitch终止 TLS。

在 Varnish 中启用 PROXY 协议支持是通过打开一个特定的监听地址来完成的:

varnishd -a :80 -a :8443,PROXY

8443然后 Varnish 可以通过 PROXY 协议接受端口上的连接。

使用 PROXY 协议的主要优点是原始客户端 IP 地址一直传输到 Varnish。无论 Varnish 前面有多少个代理,其client.ip值始终是原始客户端的 IP 地址。

如果 Pound 不支持 PROXY 协议,我建议你切换到Hitch

定义 ACL

一旦您设法使用 PROXY 协议支持设置 TLS 终止,您可以编写一些 VCL 来从缓存中传递项目,如下所示:

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

推荐阅读