Wednesday, November 27, 2013

Nginx as a proxy passthrough for BOSH

Nginx as a proxy passthrough for BOSH

In a past post I talked about using nodejs and http-proxy as a way of routing your request to internal handlers. One thing that we want to do when working with XMPP from the web is to proxy request to a BOSH server running on another port. There are issues that arise here from Cross Origin Scripting or COORS. Basically if (stophe.js) does not make the BOSH request from the same domain and port, it will fail.

To get around this I added another rule into my nodejs routes. The rule looked something like the following.

var http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server
//

var options = {
  router: {
    'domain.com/http-bind/': 'localhost:7070/http-bind/',
    'domain.com' : 'localhost:9000',    
  }
};

httpProxy.createServer(options).listen(80);


This did not work. I figure it has something to do with not passing through the correct host header.. thus still running into a CORS issue.

Solution Nginx

Download and install nginx What was required was to setup nginx to allow proxy-passthrough to BOSH. You need to edit your nginx.conf file and include something similar to the following.

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
  location /http-bind/ {
   proxy_pass http://localhost:7070/http-bind/;
  }

        location / {
    proxy_pass http://localhost:9000/;
    proxy_set_header Host $http_host;
  }
}


In this case we are taking traffic from port 80 and either mapping it to our Play Server (port 9000) or to the openfire BOSH connection (http://localhost:7070/http-bind/). This worked :)

No comments:

Post a Comment