Question

I have partially developed a property website that fetch properties data from a RETS IDX. You may know that RETS server listened to port 6103 over http protocol. My website is deployed on a shared hosting due to which I can not connect to 6103 port. I do have a dedicated server (which allows connect to port 6103). I want to use this dedicated server as a middle tier between my website and the RETS IDX server. My problem is I want to develop that middle tier script i.e HTTP Tunnel.

My website will send all RETS request to this Tunnel that will meanwhile sent it to the RETS IDX server and its response will be sent back to the website at the same moment.

                         port 80                                 port 6103

Website (shared hosting) ----------> Tunnel (Dedicated hosting)  -----------> RETS Server

RETS Server also requires to login, so the session should be maintained properly.

I want to have quick/best solution to do the job. May be through .htaccess or streaming php script or may be some third party script can also cut some of my time.

I would love to hear any thought or suggestion you have.

P.S: I can not move my website to a dedicated server because in near future I am going to have plenty of them and they would cost too much.

Was it helpful?

Solution

I'd personally go for the Reverse Proxy approach. This will allow you to intelligently forward requests, based on configurable criteria.

Both Apache and nginx have reverse proxy capabilities (in fact it was nginx's original purpose). For Apache you need to use mod_proxy, while nginx has the functionality built in unless you explicitly disable it before compiling.

Of these two options I personally prefer nginx, it is robust and lightweight, and completely fit for purpose. I find Apache more cumbersome, but if you already have Apache set up on your dedicated server, you may prefer to use that instead.

The beauty of using web servers to proxy, is that they understand the underlying protocol. They will preserve headers, modify cookies (preserve sessions), and translate hostnames correctly.


Apache Config

In both cases configuration is very straightforward, the Apache config looks something like the following:

<Location /proxy-location/>
    ProxyPass /rets http://rets-server:6103/api/path
    ProxyPassReverse /rets http://rets-server:6103/api/path
</Location>

There's also options for tweaking cookies, setting timeouts etc. All of which can be found in the mod_proxy documentation

You should note that this cannot go in a .htaccess file. It must go in the main server config.


nginx Config

Equally as simple

location /proxy-location {
    proxy_pass        http://rets-server:6103/api/path;
}

Again tons of options in the HttpProxyModule documentation for caching, rewriting urls, adding headers etc.


Please do consult the docs. I've not tested either of these configurations and they may be a little off as they're from memory + a quick google.

Make sure you test your app by proxying to an unreachable server and ensure it handles failures correctly since you are introducing another point of failure.

I'm working on the assumption you are able to configure your own dedicated server. If this is not the case, your hosts may be willing to help you out. If not leave me a comment and I'll try and come up with a more robust curl option.

OTHER TIPS

You can achieve this by using curl's PHP extension.

An example code could be :

$url = $_GET['url'];

$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($ch);

echo $content;

Obviously you have to add protection, perhaps add .htaccess/.htpasswrd protection to it.

A more complete code, with cookie support and such, can be found there : https://github.com/cowboy/php-simple-proxy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top