问题描述
我想使用Apache HTTPd作为代理服务器:
I want to use the Apache HTTPd as a proxy server:
如果用户请求http://xxx?st=yyy
,则所选的后端服务器应为server1
.如果用户请求http://xxx
(无st参数),则后端服务器应为server2
.
If a user requests http://xxx?st=yyy
the chosen backend server should be server1
. If a user requests http://xxx
(no st parameter) then the backend server should be server2
.
我想知道我需要如何配置Apache来实现这一目标.
I want to know how I need to configure Apache to achieve this.
推荐答案
看看 http://httpd.apache.org/docs/current/mod/mod_rewrite.html 和示例;具体来说,您会得到以下帮助:
Have a look at http://httpd.apache.org/docs/current/mod/mod_rewrite.html and the examples; specifically you are helped by the fact that:
- REQUEST_URI所请求URI的路径部分,例如"/index.html". 尤其是排除查询字符串,该查询字符串可以作为其自己的名为QUERY_STRING的变量来使用.
- REQUEST_URIThe path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.
然后让您做类似的事情
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/foo/(.*)$ http://server2/$1 [P,L]
RewriteRule ^/foo/(.*)$ http://server1/$1 [P,L]
,依此类推.如果是整个服务器-删除/foo/,在$ 1之前/,如果它是服务器特定的,则在其前面放一个额外的RewriteCond以限制到特定的主机,依此类推.
and so on. If it is the entire server - remove /foo/ and the / before $1 - if it is server specific - put an extra RewriteCond in front of it to limit to a specific host and so on.
这篇关于基于URL中参数的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!