1

Let me explain it more here -- I am using BOX1, I want to visit BOX2, but I want to use a domain to visit it(example.com), so my domain is pointing to box1, now how do I get example.com to fetch content from box2 without actually redirecting to IP address of box2?

user1770015
  • 185
  • 2
  • 3
  • 9

3 Answers3

1

I figured it out thanks to this post: http://techzinger.blogspot.ca/2007/07/writing-reverse-proxy-in-php5.html

user1770015
  • 185
  • 2
  • 3
  • 9
0

Our definition of 'reverse proxy' seems to differ. I tend to towards equating it with load balancing and caching, while you simply want something to issue 301/302 redirects based on a GeoIP lookup.

<?php
$servers = array(
    'CA' => array('1.1.1.1', '2.2.2.2'),
    'US' => array('3.3.3.3', '4.4.4.4')
);

$cc = geoip_country_code_by_name( $_SERVER['REMOTE_ADDR'] );

if( in_array($cc, $servers) ) {
    $server = $servers[$cc][rand(0,count($servers[$cc]))];
} else {
    $server = '5.5.5.5';
}

header('Location: ' . $server);
exit();
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • I want to mask the server ip, not redirect it. EX: example.com loads to lets say a US server, then example.com/page2.php still loads to same server, but does not redirect to the ip address to load it – user1770015 Dec 05 '12 at 22:50
  • 1
    @user1770015 then there is absolutely *zero point* in having servers in other countries. *At best* you will have the same network performance as between the user and the 'proxy', *plus* whatever network delay is between the 'proxy' machine and the server. – Sammitch Dec 05 '12 at 22:53
  • there may be zero point to you, but there is a point to me. now do you know how or not? – user1770015 Dec 05 '12 at 22:55
  • Nope. No idea. I am not a magician, so I cannot perform the particular form of sorcery you require. – Sammitch Dec 05 '12 at 22:57
  • would a curl call be possible? – user1770015 Dec 05 '12 at 22:58
0

You can write a very simple reverse proxy in PHP. You basically just take the request parameters and pass them on using curl or file_get_contents. For example:

<?php
  $external_url = 'http://www.anotherserver.com' . $_SERVER['REQUEST_URI'] . $_REQUEST['QUERY_STRING'];
  print file_get_contents($external_url);
?>

Check out this article for more details.

Kogs
  • 82
  • 6
  • 2
    this may be simple but it doesn't include any kind of headers, cookies, etc. and will very quickly be deemed to be spam. – Jacob Kranz May 06 '14 at 21:22