簡體   English   中英

基於請求URI長度的Apache ProxyPass

[英]Apache ProxyPass Based on Request URI Length

我在嘗試將https://domain.com/ {6個字符的密鑰}代理到另一台服務器上的Apache ProxyPass(更具體地說是ProxyPassMatch)遇到問題。

我已經嘗試過如下所示的正則表達式(嘗試考慮多種可能由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作為第二組括號,等等。

例如

   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,}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM