我想将单个请求转发到同一服务器上的多个端口。我尝试在同一个 VirtualHost 文件上使用具有不同配置的 ProxyPass 和 ProxyPassReverse,但它只转发到一个端口。我也尝试使用 proxy_balancer,但仍然只将请求转发到一个端口。我是否遗漏了什么,或者这可能不是实现此目的的最佳方法?
代码:
<Proxy balancer://mycluster>
#1st port
BalancerMember http://127.0.0.1:8000/
#2nd port
BalancerMember http://127.0.0.1:8001/
ProxySet lbmethod=bytraffic
</Proxy>
<Location /app>
ProxyPass balancer://mycluster/
ProxyPassReverse balancer://mycluster/
</Location>
我也尝试过:
<Location /app>
ProxyPass http://127.0.0.1:8000/
ProxyPassReverse http://127.0.0.1:8000/
</Location>
<Location /app>
ProxyPass http://127.0.0.1:8001/
ProxyPassReverse http://127.0.0.1:8001/
</Location>
答案1
使用 ReactPHP 服务器的示例脚本将响应转发到多个端口..
<?php
require_once 'vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$ports = ["8001", "8002", "8003"];
$http_client = new \Clue\React\Buzz\Browser($loop);
$server = new \React\Http\Server($loop, function (\Psr\Http\Message\ServerRequestInterface $request) use ($http_client, $clients){
# forward requests to various apps on ports
$body = $request->getBody()->getContents();
foreach($ports as $port){
$http_client->post("http://127.0.0.1:" . $port, ['Content-Type' => 'application/json'], $body);
}
//echo $request->getBody();
return new \React\Http\Message\Response(200, ["Content-Type" => "text/plain"], "request forwarded to ports\n");
});
$socket = new \React\Socket\Server('127.0.0.1:8000', $loop);
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp', 'http', $socket->getAddress()) . PHP_EOL;
$loop->run();