Nginx-如果引用,将(x-forwarded-for)ip添加到黑名单中

问题描述 投票:0回答:1

我已经写了一个基本的http Referer重定向器,看起来像这样:

if ($http_referer ~* (google|yahoo|bing|duckduckgo)) {  return 301 https://altavista.com; }

我的目标是这样的

if ($http_referer ~* (google|yahoo|bing|duckduckgo)) {
    add $x-forwarded-for bad_ips.txt; <-- this line is the question (x-forwarded-for because it's behind cloudflare)
    return 301 https://altavista.com; 
}
if ($bad_ip) { 
    return 301 https://altavista.com; 
}

有什么办法吗?从本质上来说,目标是如果您从黑名单上的网站获得推荐,我想将您的IP添加到重定向列表中。

注意:这不必纯粹在nginx中完成;只要可以免费使用除nginx之外的其他技术,就可以了。

[请注意,我的网站正在使用cloudflare,因此它必须使用x-forward-for地址,而不是禁止简单的IP,否则我只是禁止cloudflare。

谢谢

nginx redirect http-status-code-301
1个回答
0
投票

直到我记得没有直接方法可以做到这一点。

根据this博客,付费选项可以使用Nginx Plus。

除此之外,我现在可以看到两个选项:

  1. 与LUA模块一起使用OpenResty。您要做的是

    1. 根据每个请求将IP写入文件
    2. 对于从文件读取的每个请求,如果IP存在,则301重定向。

      [我现在关注效率。此方法可能会增加性能开销,因为必须在每个请求上都对文件进行解析。]

  2. 与Nginx一起使用Fail2Ban。

I wanted to add this in the comment but I don't have enough reputation to comment.

© www.soinside.com 2019 - 2024. All rights reserved.