Home
ASP XML   Apache Proxy   PHP Reverse Proxy with Apache   Search Page  
search > PHP Reverse Proxy with Apache
   create account

 
    (advanced)

Howto: Run on an Apache server with PHP support

This works by getting Apache to redirect all page requests in a particular webserver directory to a PHP file. The PHP file then does a reverse proxy to Net Research Server, requesting the page and passing it on.

Step 1: Apache Redirect
Place a file called .htaccess in the root of your Apache website (often /var/www/html) with the following redirect code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+)$ index.php/$1 [QSA]
Give permissions to your .htaccess file:
chmod 644 .htaccess

Make sure the .htaccess file settings are used. Check that the Apache configuration file (httpd.conf or other) has the following option for the root directory of the website:
AllowOverride All


Step 2: PHP Script
Create an index.php file in the website root directory and place the following code inside:
<?php
$proxy_url = "http://demo.loopip.com";

if (isset($_SERVER['PATH_INFO']))
    $proxy_url .= $_SERVER['PATH_INFO'];
else
    $proxy_url .= '/';
if ($_SERVER['QUERY_STRING'] !== '')
    $proxy_url .= "?{$_SERVER['QUERY_STRING']}";
$cu = curl_init($proxy_url);
function read_header($ch,$string) { header(rtrim($string)); return strlen($string); }
function read_body($ch,$string)   { echo $string; return strlen($string); }
curl_setopt($cu,CURLOPT_RETURNTRANSFER, true);
curl_setopt($cu,CURLOPT_BINARYTRANSFER, true); 
curl_setopt($cu,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($cu,CURLOPT_WRITEFUNCTION, 'read_body');
curl_setopt($cu,CURLOPT_HEADERFUNCTION, 'read_header');
if (count($_POST))
{
    $post=array();
    foreach($_POST as $key=>$value)
        $post[] = urlencode($key)."=".urlencode($value);
    curl_setopt($cu,CURLOPT_POST, true);
    curl_setopt($cu,CURLOPT_POSTFIELDS, implode('&',$post));
    unset($post);
}
elseif ($_SERVER['REQUEST_METHOD'] !== 'GET') 
    curl_setopt($cu,CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
$headers = apache_request_headers();
$client_headers = array();
foreach ($headers as $header => $value) 
	$client_headers[] = sprintf('%s: %s', $header, $value);
$client_headers[] = sprintf('X-Forwarded-For: %s', $_SERVER['REMOTE_ADDR']);
curl_setopt($cu,CURLOPT_HTTPHEADER, $client_headers);
curl_exec($cu);
if (curl_errno($cu) != 0)
	echo curl_error($cu);
?>
Make sure the $proxy_url variable contains the correct domain/ip and port under which Net Research Server is running.

Step 3: Allow Apache Scripts Network Access
Sometimes Apache is configured to not allow scripts any network access. You can turn this off by running the following commands:
setsebool -P httpd_disable_trans 1
service httpd restart

Step 4: Test It
Visit your website and use the same path of this template, eg. http://www....mywebsite....com/integration_demo/search/php_reverse_proxy.
Some urls might not resolve properly. You can fix that by specifying the same host as your website in the default template of your application in the host field.





 

Help build the largest human-edited directory on the Web.
Submit a Site - Open Directory Project - Become an Editor

(c) Copyright 2008, LoopIP LLC. All Rights Reserved