Skip to content

Instantly share code, notes, and snippets.

@CodingNinja
Created May 31, 2012 13:29
Show Gist options
  • Save CodingNinja/2843433 to your computer and use it in GitHub Desktop.
Save CodingNinja/2843433 to your computer and use it in GitHub Desktop.
Remote Proxy with Varnish
# Our origin backend
backend blogs {
.host = "blogs.dev";
.port = "8888";
}
# Our proxy backend
backend remote {
.host = "proxy.local";
.port = "8888";
}
sub vcl_recv {
# Default to proxy
set req.backend = remote;
# Only match what we explicitly want
if (req.http.host ~ "blogs.dev") {
set req.backend = blogs;
return (lookup);
}
# Store the old host so we know where to go get something from
set req.http.prehost=req.http.host;
# And make sure Apache knows which vhost we want
set req.http.host="proxy.local";
return (lookup);
}
sub vcl_fetch {
set beresp.ttl = 0s;
set beresp.do_esi = true;
}
<esi:include src="http://example.com/header.html" />
<esi:include src="http://example.com/body.html" />
<esi:include src="http://example.com/footer.html" />
<?php
$url = (isset($_SERVER['HTTP_PREHOST']) ? $_SERVER['HTTP_PREHOST'] : $_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI'];
echo sprintf('<!-- Reverse Proxy got "%s"-->', $url) . PHP_EOL
. curl_get($url, array(), array(CURLOPT_PROXY => '<username>:<password>@<proxy>:<port>')) . PHP_EOL
. sprintf('<!-- // End reverseproxy get of "%s" -->', $url) . PHP_EOL;
/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $get values to send
* @param array $options for cURL
* @return string
* @link http://au.php.net/manual/en/function.curl-exec.php#98628
*/
function curl_get($url, array $get = array(), array $options = array())
{
$defaults = array(
CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get),
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 4
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment