0

AWS の ELB の背後にある nginx インスタンスの小さなプールがあります。この ELB はインターネットに接続されており、HTTP ではなく PROXY プロトコルを使用します。

これが私の関連セクションです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;

denyのディレクティブを使用ngx_http_access_moduleして、一連の CIDR ブロックへのアクセスを防止しようとしています。

EG:.conf起動時にnginxによってロードされたファイル内:

include /etc/nginx/ip_block/*.conf;

dirに/etc/nginx/ip_block/は少なくとも1つのファイルがあります:

$ 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;

ただし、nginx のdenyディレクティブは変数でのみ機能し、$remote_addr変数では機能しないよう$proxy_protocol_addrです。denyこれは事実上、ディレクティブと の両方を一緒に使用できないことを意味しproxy_protocolます。

モジュールを使用して の値をngx_stream_realip_module値に調整できるように見えますが、私が利用できる nginx のビルドは現在、ビルド フラグで構成されていません。現在実行中ですが、ビルドでフラグが導入されたようです( https://github.com/nginx/nginx/commit/fe2774a9d689fa1bf201dd0e89449e3d9e4ad926 )$remote_addr$proxy_protocol_addr--with-stream_realip_module1.10.3with-stream_realip_module1.11.4

オプション 1:必要な機能をコンパイルして、ソースから nginx のバージョンをビルドします。

ディレクティブのドキュメントを見ていると、次のdenyメモが見つかりました。

In case of a lot of rules, the use of the ngx_http_geo_module module variables is preferable.

出典: https://nginx.org/en/docs/http/ngx_http_access_module.html

これは、現在持っているnginxのバイナリで動作する可能性のあるCIDR範囲をブロックするという私の目標を達成するためのより良い方法があるかどうか疑問に思います.

私はこのようなことを試すことができます:

geo $proxy_protocol_addr $blocked_cidr {
    default        01;
    include        conf/some_cidr_ranges_to_block.conf;
}

ファイルconf/some_cidr_ranges_to_block.confは次のようになります。

2604:a880:1::/48    02;
<snip>
208.68.36.0/22      02;

そして、サーバーディレクティブで次のようなことができます:

if ($blocked_cidr != 01) {
 return 403;
}

オプション 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?
4

2 に答える 2