Apache 2.4 RewriteCond with expr / ipmatch on variable other than %{REMOTE_ADDR}

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

我在 CDN 后面有一个 Web 服务器。客户端 IP 作为 HTTP 标头传递。

我可以使用以下内容来阻止客户端 IP 地址:

RewriteCond %{HTTP:CloudFront-Viewer-Address} ^123\.45\.67\.89(.*)$
RewriteRule ^(.*)$ - [F,L]

我相信

(.*)$
需要匹配
%{HTTP:CloudFront-Viewer-Address}
中可能包含的端口。我在访问日志中看到
%{HTTP:CloudFront-Viewer-Address}
之后的端口(例如 127.0.0.1:1234),但在重写日志中看不到。在重写日志中,端口附加到
%{HTTP:CloudFront-Viewer-Address}
之前的 CloudFront 和负载均衡器地址,而不是
%{HTTP:CloudFront-Viewer-Address}
本身。

CGI 程序打印如下内容:

HTTP_CLOUDFRONT_VIEWER_ADDRESS --> 0000:0000:0000:0000:0000:0000:0000:0001:52325
HTTP_CLOUDFRONT_VIEWER_ADDRESS --> 127.0.0.1:45629

我去的时候

我想用这样的东西:

RewriteCond expr "%{REMOTE_ADDR} -ipmatch '123.45.67.0/24'"
RewriteRule ^(.*)$ - [F,L]

阻止一系列 IP,如下所示:https://perishablepress.com/apache-redirect-range-ip-addresses/

但它不起作用。

我试过:

RewriteCond expr "%{HTTP:CloudFront-Viewer-Address} -ipmatch '123.45.67.0/24'"
RewriteRule ^(.*)$ - [F,L]

我不确定是不是因为

%{REMOTE_ADDR}
是ipmatch唯一可以处理的变量;或我的变量中可能包含的端口;或其他一些原因。

假设它是端口,并且 ipmatch 可以作用于

%{HTTP:CloudFront-Viewer-Address}
,是否可以从
%{HTTP:CloudFront-Viewer-Address}
中删除端口允许它工作?

我尝试使用此处的建议来做到这一点:https://www.reddit.com/r/apache/comments/11bxlxi/cidr_matching_rewrite_with_expr_ipmatch_remove/ 但无法正常工作。

感谢您的任何建议!

apache mod-rewrite
1个回答
1
投票

我在这里建议的模式是预先使用 SetEnvIf 对其进行操作,并将结果保留在环境变量中。

SetEnvIf CloudFront-Viewer-Address (.*):\d+$ cf-v-a=$1
RewriteCond expr "%{ENV:cf-v-a} -ipmatch '123.45.67.0/24'"
RewriteRule ^(.*)$ - [F,L]
© www.soinside.com 2019 - 2024. All rights reserved.