Apache 2.4使用expr / ipmatch对%{REMOTE_ADDR}以外的变量重写Cond

5lwkijsr  于 2023-05-07  发布在  Apache
关注(0)|答案(1)|浏览(175)

我在CDN后面有一个Web服务器。客户端IP作为HTTP标头传递。
我可以使用以下命令来阻止客户端IP地址:

RewriteCond %{HTTP:CloudFront-Viewer-Address} ^123\.45\.67\.89(.*)$
RewriteRule ^(.*)$ - [F,L]
  • 我认为(.*)$需要匹配%{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/但无法让它工作。
感谢您的任何建议!

fumotvh3

fumotvh31#

我在这里建议的模式是使用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]

相关问题