Deploying web-socket support monitoring beside nginx – Debian/ubuntu 12.10

Required packages :

root privileges are needed to complete the installation

$ sudo su

peform a random check to confirm if the required packages are insatlled .

$ apt-show-versions | grep openssl
  $ apt-show-versions | grep zlib
  $ apt-show-versions | grep pcre
  $ apt-show-versions | grep patch

 proceed with installation

$ apt-get install build-essential libssl-dev openssl openssl-devel
  $ sudo su
  $ cd ~
  $ mkdir -p ~/nginx_patched
  $ cd ~/nginx_patched
  $ export NGINX_VERSION=1.2.5
  $ downloading nginx source
  $ curl -O http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
  $ clonning proxy module for nginx from git repo
  $ git clone https://github.com/yaoweibin/nginx_tcp_proxy_module.git
  $ extracting nginx source files
  $ tar -xvzf nginx-$NGINX_VERSION.tar.gz
  $ patching nginx
  $ cd nginx-$NGINX_VERSION
  $ patch -p1 < ../nginx_tcp_proxy_module/tcp.patch
after downloading and patching the nginx package with the appropriate patch you can compile it with various options
  $ ./configure
  -prefix=/usr \
  --conf-path=/etc/nginx/nginx.conf \
  --http-log-path=/var/log/nginx/access.log \
  --error-log-path=/var/log/nginx/error.log \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/lock/nginx.lock \
  --user=www-data \
  --group=www-data \
  --http-client-body-temp-path=/var/tmp/nginx/client \
  --http-proxy-temp-path=/var/tmp/nginx/proxy \
  --with-http_ssl_module \
  --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
  --add-module=/root/nginx_patched/nginx_tcp_proxy_module

after which you are ready to install

$ make && make install

confirm successful installation

$ nginx -v

in the output you should see:

  $ nginx version: nginx/1.2.5

since there was “www-data” directive in the nginx configuration you need to create a user; adding user

$ adduser --system --no-create-home --disabled-login --disabled-password --group www-data

configure nginx using your favorite editor

$ vim /etc/nginx/nginx.conf

and make it simmilar to the file below;

——————————————————————————————————————————————————————————————————————–

worker_processes  1;
events {
  worker_connections  1024;
  }
tcp {
  upstream websockets {
  ## node processes
  server 127.0.0.1:3000;
  check interval=3000 rise=2 fall=5 timeout=1000;
  }
server {
  listen 8080;
  #server_name _;
access_log /var/log/nginx/tcp_access.log;
tcp_nodelay on;
  proxy_pass websockets;
  }
}
http {
  include       mime.types;
  default_type  application/octet-stream;
sendfile        on;
  keepalive_timeout  65;
  gzip  on;
include /etc/nginx/sites-enabled/*;
## status check page for websockets
  server {
  listen 9000;
access_log /var/log/nginx/tcp_access.log;
location / {
  check_status;
  }
  }
  }
testing web socket application
vim app.js
var http = require('http'),
  url = require('url'),
  fs = require('fs'),
  socket = require('socket.io'),
  httpServer = http.createServer(onRequest),
  port = process.argv[2] || 3000;
function onRequest(request, response) {
  var parsedUrl = url.parse(request.url),
  pathname = parsedUrl.pathname;
pathname = ((pathname === '/') ? '/index.html' : pathname);
  pathname = '/public' + pathname;
fs.readFile(__dirname + pathname, function (err, data) {
if (err) {
  console.log('app.onRequest() [ ERROR ] ' + pathname + ' doesn\'t exist');
headers = {'Content-Type':'text/plain'};
  response.writeHead(404, headers);
  response.write('Error occurred!');

} else {

if (/\.css$/.test(pathname)) {
  headers = {'Content-Type':'text/css'};
  } else {
  headers = {'Content-Type':'text/html'};
  }

response.writeHead(200, headers);
  response.write(data);
  }

response.end();

});

}

httpServer.listen(port); console.log(“Server has started on : ” + port); socket.listen(httpServer);

————————————————————————————————————————————————————————————

testing a demo of our configured website includes creating a sample file in the public html folder and pasting sample code below ;

create a public folder if does not exist!

$ mkdir public
   $ vim public/index.html

——————————————-

html>
  
  Websockets Proxy Test
  // <!--[CDATA[
 src="/socket.io/socket.io.js">
 // ]]>
 
 var socket = io.connect('http://localhost:8080');
 socket.on('news', function (data) {
 console.log(data);
 socket.emit('my other event', { my: 'data' });
 });
 

—————————————————————————————————————————————————–

Websockets Proxy Test

within the created file you should be able choose which port eg 8888, 8080, 8081 ecetera
var socket = io.connect(‘http://localhost:8080&#8217;);

Run the server

$restart nginx
$pkill nginx
$ nginx
$ run application
$ node app.js

You should see output within the terminal

$ Server has started on : 3000
 $  info  – socket.io started

And if you navigate to http://localhost:8080/ you should see “Websockets Proxy Test” in browser window

if you navigate to http://localhost:9000/ you will be able to see a websocket monitor.

Advertisements