简体   繁体   English

基于请求URI长度的Apache ProxyPass

[英]Apache ProxyPass Based on Request URI Length

I've got a problem with Apache ProxyPass (more specifically ProxyPassMatch) where I'm trying to proxy https://domain.com/ {6 character key} to another server. 我在尝试将https://domain.com/ {6个字符的密钥}代理到另一台服务器上的Apache ProxyPass(更具体地说是ProxyPassMatch)遇到问题。

I've tried regex's like the following (attempting to take into consideration multiple ways that this might be handled by Apache): 我已经尝试过如下所示的正则表达式(尝试考虑多种可能由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}

However nothing seems to work. 但是,似乎没有任何效果。 Any help on this matter would be greatly appreciated. 在此问题上的任何帮助将不胜感激。

The fix 解决方法

You need to capture the characters in the pattern and then reference them in the url: 您需要捕获模式中的字符,然后在url中引用它们:

Example from the docs: 来自文档的示例:

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

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

So in your case I think what you want is: 因此,在您的情况下,我认为您想要的是:

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


Why/How this works 为什么/如何运作

When using regular expressions, you can capture matched text by using brackets and then reference them using $1 for the first set of brackets, $2 for the second etc. 当使用正则表达式时,您可以使用方括号捕获匹配的文本,然后使用$ 1作为第一组括号,使用$ 2作为第二组括号,等等。

eg 例如

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

Would match http://domain.com/123456/ABCDEF and proxy to https://domain.com/api/ABCDEF/123456 将匹配http://domain.com/123456/ABCDEF并代理到https://domain.com/api/ABCDEF/123456


One more thing 还有一件事

Also note you don't need {6,6} and can just use {6} to say you need the character to match exactly 6 times, you use this format where you want a variable number of characters eg {4,6} for between 4 and 6 - you can also specify {4,} for 4 or more. 还要注意,您不需要{6,6}而只需使用{6}来表示您需要将该字符精确匹配6次,则在需要可变数量字符的情况下使用此格式,例如{4,6}在4到6之间-您还可以为4或更多指定{4,}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM