0

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.

FatalError
  • 922
  • 12
  • 31

2 Answers2

3

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.

Leigh
  • 12,859
  • 3
  • 39
  • 60
  • Perfect! This is exactly what I was looking for. I have implemented it locally on my machine. It work good for the first couple of request (signing in) after that I get 404 error. May be we need to add some parameter to keep the connection alive? or may be it is not passing the cookies/session? – FatalError Jul 24 '12 at 12:15
  • 1
    @FatalError: You'll have to hit the log files and find out exactly what the 404 was for. Was it a 404 on your proxying server because a redirect was returned and it didn't cater for it, or was it a 404 on the RETS server and it just got forwarded back to you. Set logging to stun, and be prepared to write a bunch of rules to cater for everything :) – Leigh Jul 24 '12 at 12:17
  • Proxy is not working for multipart contents i.e. images. Can you look at http://serverfault.com/questions/410730/apache-proxypass-rewriterule-with-p-flag-return-no-content-when-response-has-mu ? – FatalError Jul 27 '12 at 12:29
0

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

magnetik
  • 4,351
  • 41
  • 58
  • It wont send the received headers to $url. Also, it does not have the ability to save session. I want the script to send every received information to $url or forwarding url i.e. headers, session, cookies, http authentication, everything with a single difference of the url and port number. – FatalError Jul 23 '12 at 10:05