繁体   English   中英

Apache重写请求使用不同的转发代理

[英]Apache rewrite request using a different forward proxy

我想基于请求标头将Apache中的请求转发给不同的转发代理。 我认为最好的方法是使用mod_rewrite,但它只能使用在相同的apache配置中定义的反向代理。

我还检查了mod_proxy的ProxyRemote属性,但它不能根据条件使用,只能基于请求url。

我需要这样的东西:

如果X-CUSTOM-HEADER是值-1 - >转发请求转发代理p1如果X-CUSTOM-HEADER是值-2 - >转发请求转发代理p2

等等

Din有人设法制作这样的东西吗?

谢谢,艾琳

我找到了一个解决方案,它不是很优雅。 它还涉及第二台服务器上的一些调整。

它源于我遇到类似问题的项目,但需要服务器“完全”(由使用数据库资源的自定义脚本选择)。

这应该至少有效(我通过重写映射运行我的URL来修改它,我使用RewriteCond调整它以使用标头)。

# example for server number "5" in your remote proxy network
RewriteCond %{HTTP:X-CUSTOM-HEADER} 1
RewriteRule http://([a-z0-9\.]+)/(.*) http://$1.5.server.yourdomain.com$1 [P] 
ProxyRemoteMatch .*\.5\.server\.yourdomain\.com.* http://5.server.yourdomain.com:80

您基本上调整了URL,使其成为第二台服务器的子域,然后再将其剥离。
这部分继续第二个(远程代理服务器):

<ProxyMatch "http://.*\.[0-9]+\.server\.yourdomain\.com/.*">
    RewriteEngine on
    RewriteRule (proxy:http[s]?://.+)\.[0-9]+\.server\.premiumize\.me(.+) $1$2 
    ... your code ...
</ProxyMatch>

试试这个:

# Prevents Apache from functioning as a forward proxy server (where you don't want)
ProxyRequests Off
# Preserve Host in http protocol on destination server
ProxyPreserveHost On
<Proxy *>
   Order deny,allow
   Allow from all
</Proxy>
# enable rewrite engine
RewriteEngine On 
# check header
RewriteCond %{HTTP:X-CUSTOM-HEADER} 1
# execute forward proxy
RewriteRule (.*) http://server1/$1 [P,L,QSA]

# check header
RewriteCond %{HTTP:X-CUSTOM-HEADER} 2
# execute forward proxy
RewriteRule (.*) http://server2/$1 [P,L,QSA]

你应该能够通过使用%{HTTP:header}验证的RewriteCond指令来实现它。

请尝试以下方法:

RewriteEngine On 

RewriteCond %{HTTP:X-CUSTOM-HEADER} 1
RewriteRule (.*) http://p1.example.com$1 [P] 
ProxyPassReverse / http://p1.example.com

RewriteCond %{HTTP:X-CUSTOM-HEADER} 2
RewriteRule (.*) http://p2.example.com$1 [P] 
ProxyPassReverse / http://p2.example.com

希望能帮助到你。 :)

暂无
暂无

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

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