【问题标题】:Apache ProxyPass only if specific string matchs in subdomain仅当子域中的特定字符串匹配时才使用 Apache ProxyPass
【发布时间】:2019-05-01 20:30:48
【问题描述】:
我使用 apache 作为反向代理,只有当我的子域匹配特定字符串时,是否有任何方法 ProxyPass,例如:
<VirtualHost *:80>
ServerAlias *.example.com
<If "%{HTTP_HOST} -strmatch 'hello*.example.com'">
ProxyPreserveHost On
ProxyPass "/" "http://192.168.1.22/"
ProxyPassReverse "/" "http://192.168.1.22/"
</If>
</VirtualHost>
无法在“If”中使用 ProxyPass,是否有任何工作可以实现相同的目标?
谢谢!
【问题讨论】:
标签:
apache
redirect
reverse-proxy
virtualhost
httpd.conf
【解决方案1】:
我会尝试使用 mod_rewrite 和 Proxy 指令。
<VirtualHost *:80>
ServerName *.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hello*\.example\.com$
RewriteRule ^/(.*)$ http://192.168.1.22/$1 [proxy]
ProxyPassReverse / http://192.168.1.22/
</VirtualHost>
【解决方案2】:
如果可能的话,我认为您可以使用 Regex 或 RewriteCond,我将解释我对这个问题的解决方案:
由于您希望匹配完全相同的字符串,您可以排除除您要查找的字符串之外的所有内容,如果可用,我已经为您编写了 RewriteCond:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*\.example\.com[NC]
RewriteCond %{HTTP_HOST} !=hello.example.com[NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
这段代码应该可以工作,如果不行,我将搜索一个正则表达式条件以使用不同的方法创建相同的模式,排除每个子域,除了与 hello.example.com 匹配的子域
我也有这个其他代码
RewriteCond %{HTTP_HOST} (^|\.)example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(hello)\.example\.com$ [NC]
RewriteRule ^ http://hello.example.com%{REQUEST_URI} [NE,R=301,L]