我正在尝试创建一个与 Apache 2.2 中除一个 URL 之外的所有 URL 匹配的反向代理。它在 Apache 2.4 (Centos 7) 上有效,但在 Apache 2.2 (Centos 6.6) 上无效...
# Do not modify this
<LocationMatch "^/my-website-2">
ProxyPass http://X.X.X.X:PORT/my-website-2
ProxyPassReverse http://X.X.X.X:PORT/my-website-2
(...)
</LocationMatch>
# Match every URL except the one finishing with /redirect
<LocationMatch "^/my-website(?!/redirect)">
ProxyPass http://X.X.X.X:PORT/my-website
ProxyPassReverse http://X.X.X.X:PORT/my-website
AuthType XXX
RequestHeader set XXX YYY
(...)
</LocationMatch>
# Do anothers directives with this URL only, finishing with /redirect
<Location "/my-website/redirect">
AuthType XXX
(...)
</Location>
当我输入时,我的服务器正在寻找 /var/www/html/my-website(不存在)https://my-server.com/my-website因为正则表达式与 ^/my-website(?!/redirect) 不匹配
我知道 Apache 2.2 不能理解每个负 PCRE 正则表达式,但似乎存在一些技巧...请参阅:
- Apache 位置指令 - PCRE 否定组合(匹配所有但...)
- https://stackoverflow.com/questions/3404037/how-to-make-a-regex-for-files-ending-in-php
- https://stackoverflow.com/questions/8545680/how-to-tell-apache-to-locationmatch-opposite-of-this
然后,我尝试一个简单的正则表达式:
<LocationMatch "/my-website(.*)">
... 而且看起来甚至没有被解释为 PCRE ... 在这个用例中,如果我在 URL 中输入 /my-website(.*),它就可以起作用。
与(From 的行为相同)http://httpd.apache.org/docs/2.2/en//mod/core.html#locationmatch):
<LocationMatch "/(extra|special)/data">
...我需要输入http://my-server.com/(extra|special)/data在浏览器的 URL 栏中。
Apache HTTPd 2.2 是否需要附加包才能理解 LocationMatch 中的 PCRE?
已安装的软件包:
httpd.x86_64 2.2.15-60.el6.centos.6
apr.x86_64 1.3.9-5.el6_9.1 @Default_Organization_CentOS_6_CentOS_6_Update_x86_64
apr-util.x86_64 1.3.9-3.el6_0.1 @in-std
pcre.x86_64 7.8-7.el6 @Default_Organization_CentOS_6_CentOS_6_Base_x86_64
pcre-devel.x86_64 7.8-7.el6 @Default_Organization_CentOS_6_CentOS_6_Base_x86_64
诡异的 ...
谢谢
答案1
您需要使用ProxyPassMatch
而不是ProxyPass
。
根据上面的例子进行了更新,并进行了其他重要更改:
# Do not modify this
<LocationMatch "^/my-website-2">
ProxyPassMatch http://X.X.X.X:PORT
ProxyPassReverse http://X.X.X.X:PORT
(...)
</LocationMatch>
# Match every URL except the one finishing with /redirect
<LocationMatch "^/my-website(?!/redirect)">
ProxyPassMatch http://X.X.X.X:PORT
ProxyPassReverse http://X.X.X.X:PORT
AuthType XXX
RequestHeader set XXX YYY
(...)
</LocationMatch>
# Do anothers directives with this URL only, finishing with /redirect
<Location "/my-website/redirect">
AuthType XXX
ProxyPassMatch http://X.X.X.X:PORT
ProxyPassReverse http://X.X.X.X:PORT
(...)
</Location>