Skip to content

Instantly share code, notes, and snippets.

@CodingNinja
Created July 2, 2012 06:58
Show Gist options
  • Save CodingNinja/3031559 to your computer and use it in GitHub Desktop.
Save CodingNinja/3031559 to your computer and use it in GitHub Desktop.
backend blogs {
.host = "blogs.dev";
.port = "80";
}
backend remote {
.host = "proxy.ying";
.port = "80";
}
sub vcl_recv {
set req.backend = remote;
if (req.http.host ~ "blogs.dev") {
#You will need the following line only if your backend has multiple virtual host names
set req.backend = blogs;
return (pass);
}
set req.http.prehost=req.http.host;
set req.http.host="proxy.ying";
return (lookup);
}
sub vcl_fetch {
set beresp.ttl = 0s;
set beresp.do_esi = true;
}
<?php
$url = (isset($_SERVER['HTTP_PREHOST']) ? $_SERVER['HTTP_PREHOST'] : $_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI'];
$url = str_replace('esi=false', 'esi=true', $url);
$url = str_replace('.dev', '.dev:8888', $url);
// echo sprintf('<!-- Reverse Proxy got "%s"-->', $url);
echo curl_get($url, array(), strpos($url, '.com.au') === false ? array() : array(CURLOPT_PROXY => 'localhost:8080'));
// echo sprintf('<!-- // End reverseproxy get of "%s" -->', $url);
/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $get values to send
* @param array $options for cURL
* @return string
*/
function curl_get($url, array $get = array(), array $options = array())
{
$defaults = array(CURLOPT_URL => $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get), CURLOPT_HEADER => 1, 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));
}
$status = curl_getinfo($ch);
list($header, $result) = explode("\r\n\r\n", $result, 2);
$headers = http_parse_headers($header);
if (isset($headers['Content-Type'])) {
header('Content-Type: ' . $headers['Content-Type']);
}
curl_close($ch);
return $result;
}
function http_parse_headers($headers = false)
{
if ($headers === false) {
return false;
}
$headers = str_replace("\r", "", $headers);
$headers = explode("\n", $headers);
foreach ($headers as $value) {
$header = explode(": ", $value);
if (isset($header[0]) && !isset($header[1])) {
$headerdata['status'] = $header[0];
} elseif ($header[0] && $header[1]) {
$headerdata[$header[0]] = $header[1];
}
}
return $headerdata;
}
Davids-MacBook-Pro:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment