使用proxy_协议时,在nginx中阻止CIDR范围的更好方法是什么?

使用proxy_协议时,在nginx中阻止CIDR范围的更好方法是什么?,nginx,proxy-protocol,real-ip,Nginx,Proxy Protocol,Real Ip,我有一小群nginx实例,它们位于AWS中的ELB后面。该ELB面向internet,使用代理协议,而不是HTTP 这是我的main.vhost的相关部分 server { # speak only PROXY protocol # Accept 80/443; we'll do the http -> https re-dir elsewhere in conf # SSL is default mode listen 443 default_serve

我有一小群nginx实例,它们位于AWS中的ELB后面。该ELB面向internet,使用代理协议,而不是HTTP

这是我的
main.vhost的相关部分

server {

    # speak only PROXY protocol
    # Accept 80/443; we'll do the http -> https re-dir elsewhere in conf
    # SSL is default mode
    listen 443 default_server ssl proxy_protocol;
    listen 80 proxy_protocol;
我试图使用来自
ngx\u http\u access\u模块的
deny
指令来阻止对一系列CIDR块的访问

例如: 在nginx启动时加载的
.conf
文件中:

include /etc/nginx/ip_block/*.conf;
/etc/nginx/ip_块/
目录中,至少有一个文件:

$ cat /etc/nginx/ip_block/some_cidr_ranges.conf | wc -l
361
$ head /etc/nginx/ip_block/some_cidr_ranges.conf
deny 2604:a880:1::/48;
<snip>
deny 208.68.36.0/22;
发件人:

这让我想知道是否有更好的方法来实现我的目标,阻止CIDR范围,这可能与我目前拥有的nginx二进制文件一起工作

我可以试试这样的东西吗:

geo $proxy_protocol_addr $blocked_cidr {
    default        01;
    include        conf/some_cidr_ranges_to_block.conf;
}
其中文件
conf/some\u cidr\u范围从\u到\u block.conf
如下所示:

2604:a880:1::/48    02;
<snip>
208.68.36.0/22      02;
选项2:尝试使用
geo
指令和自定义IP范围->“国家代码”数据库阻止通信

我的问题:

- Is *option 1* going to be a better use of time / is it worth it to build my own version of nginx with the necessary `stream_realip_module` compiled in or is it going to be more performant / effective to use the `geo` directive to map the `$proxy_protocol_addr` onto a set of ranges as shown above (*option 2*) 


- Is there some other way to block or filter traffic in nginx by cidr block when using nginx in `proxy_protocol` mode that i have not yet considered?

快速更新/回答我的问题

感谢谭宏达的建议,我没有必要编译自己的nginx版本。这使选项1从表中删除,并为我节省了一些工作。谢谢

我已经在nginx中加载了几百条
deny
规则,我正在关注性能影响

此外,我还使用
geo
指令从OP中实现了类似选项2的功能。我同样在监测影响

到目前为止,我还不清楚为什么nginx文档会建议使用带有“很多”规则的
deny

In case of a lot of rules, the use of the ngx_http_geo_module module variables is preferable.
geo
指令允许我更灵活地选择将用户发送到哪里。
deny
指令是403指令,不能更改。

您可以使用-

if($proxy\u protocol\u addr!=a.b.c.d){
返回403;


<代码> } /代码>

您也可以考虑使用<代码> StIdRealEpIpI/<代码>,<代码> RealAuppi头> <代码> > RealEpIpRealOut/<代码>设置“右”代码> $ReleTydAdDr < /代码> . @ TaangHuang-TAT-谢谢!我不确定我怎么会在文档中漏掉这个。。。在这里发布之前,我花了相当多的时间通过它寻找解决方案。
real\u ip\u header proxy\u protocol
configuration指令看起来会产生奇迹!
- Is *option 1* going to be a better use of time / is it worth it to build my own version of nginx with the necessary `stream_realip_module` compiled in or is it going to be more performant / effective to use the `geo` directive to map the `$proxy_protocol_addr` onto a set of ranges as shown above (*option 2*) 


- Is there some other way to block or filter traffic in nginx by cidr block when using nginx in `proxy_protocol` mode that i have not yet considered?
In case of a lot of rules, the use of the ngx_http_geo_module module variables is preferable.