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?
Asked
Active
Viewed 1.0k times
1
-
a little bit more info as a example. ENTRY SERVER -> get requested url, get location of visitor -> determine best location to goto -> show page – user1770015 Dec 05 '12 at 22:33
-
Possible, but I wouldn't call it advisable. – Sammitch Dec 05 '12 at 22:34
-
any ideas how? I though about cURL but then I didn't think that would work. – user1770015 Dec 05 '12 at 22:35
-
I don't see how that would be any faster. You'd have to detect the location on server A, then if TRUE redirect them to server B. The detection and redirect would take more time than just loading the page on server A. – Paul Dessert Dec 05 '12 at 22:36
-
Well, even still I want to give it a try just for fun. Any ideas how? – user1770015 Dec 05 '12 at 22:37
-
Start by reading http://stackoverflow.com/questions/297542/simplest-way-to-detect-client-locale-in-php – Paul Dessert Dec 05 '12 at 22:39
-
I already know how, already have all those scripts set up in place. – user1770015 Dec 05 '12 at 22:42
-
Then, what are you asking??? – Paul Dessert Dec 05 '12 at 22:43
-
How to use PHP to act as a reverse proxy. EX: I run a get user location on load, then using a reverse proxy like script, find the best server to go to and use it. – user1770015 Dec 05 '12 at 22:47
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20665/discussion-between-user1770015-and-relentless) – user1770015 Dec 05 '12 at 22:52
3 Answers
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
-
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
-
2this 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