基于请求URI长度的Apache ProxyPass

基于请求URI长度的Apache ProxyPass

问题描述:

我在尝试将https://domain.com/ {6字符的密钥}代理到另一台服务器时出现了Apache ProxyPass(更具体地说是ProxyPassMatch)的问题。基于请求URI长度的Apache ProxyPass

我已经试过正则表达式的如下内容(试图考虑多种方式,这可能被Apache处理):

ProxyPassMatch "/^.{22,22}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{7,7}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{14,14}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{23,23}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{21,21}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{8,8}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{6,6}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{15,15}$/g" https://domain.com/api/{6 character key} 
ProxyPassMatch "/^.{13,13}$/g" https://domain.com/api/{6 character key} 

但似乎没有任何工作。任何有关这个问题的帮助将不胜感激。

的修复

您需要捕捉模式的字符,然后在URL中引用它们:

来自实例文档:

ProxyPassMatch "^/(.*\.gif)$" "http://backend.example.com/$1" 

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassmatch

所以在你的情况我想你想要的是:

ProxyPassMatch "/^(.{6})$/" "https://domain.com/api/$1" 


为什么/这是如何工作

当使用正则表达式,您可以通过使用括号捕获匹配的文本,然后使用$ 1,第一组括号,$ 2为第二等引用它们

eg

ProxyPassMatch "/^(.{6})/(.{6})$/" "https://domain.com/api/$2/$1" 

将匹配http://domain.com/123456/ABCDEF和代理https://domain.com/api/ABCDEF/123456


一件事

还要注意你不需要{6,6}并且可以只使用{6}说你需要的字符匹配恰好6次,你可以使用这种格式,你想要可变数量的字符,例如{4,6}为4和6之间 - 你也可以指定{4,}为4或更多。