I’m trying to create a proxy site AAA.com and relay the traffic to BBB.com in the backend via PHP cURL, and again relay the response from BBB.com to AAA.com visitors. Both sites are on the same server. Below is the code:
$proxied_site = 'https://BBB.com';
$proxied_site_parsed = parse_url($proxied_site);
$ch = curl_init();
$proxied_url = $proxied_site . $_SERVER['REQUEST_URI'];
curl_setopt($ch, CURLOPT_URL, $proxied_url);
curl_setopt($ch, CURLOPT_RESOLVE, array(
"{$proxied_site_parsed['host']}:443:170.249.199.178",
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
$requestHeaders = [];
foreach (getallheaders() as $name => $value) {
if ($name == 'Host') {
$value = $proxied_site_parsed['host'];
}
$requestHeaders[] = "$name: $value";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
if (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'DELETE'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
}
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$response_header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseHeaders = substr($response, 0, $response_header_size);
$responseBody = substr($response, $response_header_size);
foreach (explode("rn", $responseHeaders) as $header_line) {
if (stripos($header_line, 'HTTP/') === 0 || stripos($header_line, 'Set-Cookie:') === 0) {
header($header_line, false);
} else {
header($header_line);
}
}
curl_close($ch);
echo $responseBody;
After some benchmarking, cURL still needs about 0.006 seconds of network time to get to the site BBB.com on the same server.
My question is, is it possible to make it faster? Can I make it lightning-fast and as fast as 0.00006 since they are both on the same server?
I tried using IP:
$ch = curl_init('XXX.XXX.XXX.XXX');
And CURLOPT_RESOLVE
:
curl_setopt($ch, CURLOPT_RESOLVE, array(
"{$proxied_site_parsed['host']}:443:XXX.XXX.XXX.XXX",
));
But neither worked and the network around time was still about 0.006s.
Is this possible with cURL at all? Or at least make it 0.0006s?
If not, why?