Nginx, node.js and socket.io - is there a working marriage?

nginx is a static killer file server.

it can serve node.js, as in this example , but in a limited way.

but nginx clearly unable to proxy websockets.

The only thing I found that might work is to use the HAProxy front end in accordance with this article - but this is from October 6, 2011.

This should be a common problem, but I do not find a very common solution.


Decision

(see https://github.com/bangkok-maco/barebone-node for a complete solution and details)

Ip testing scheme:

  • 127.0.0.12 - www.chat.nit - public, in / etc / hosts and haproxy
  • 127.0.1.12 - nginx internal web server
  • 127.0.2.12 - internal chat serving node.js socket.io

/etc/haproxy/haproxy.cfg:

global
 maxconn 4096
 nbproc 2
 daemon
 # user nobody
 log             127.0.0.1       local1 notice

defaults
 mode http

# listen on 127.0.0.12:80
frontend app
 bind 127.0.0.12:80
 mode tcp
 timeout client 86400000
 default_backend www_backend
 acl is_chat hdr_dom(Host) chat
 acl is_websocket path_beg /socket.io

 use_backend chat_socket_backend if is_websocket is_chat
 tcp-request inspect-delay 500ms
 tcp-request content accept if HTTP

# ngnix on 127.0.1.12:80
backend www_backend
 balance roundrobin
 option forwardfor
 mode http
 option httplog
 option httpclose
 timeout server 30000
 timeout connect 4000
 server w1 127.0.1.12:80 weight 1 maxconn 1024 check

# node (socket.io) on 127.0.2.12:80
backend chat_socket_backend
 balance roundrobin
 mode http
 option httplog
 option forwardfor
 timeout queue 5000
 timeout server 86400000
 timeout connect 86400000
 timeout check 1s
 no option httpclose
 option http-server-close
 option forceclose
 server s14 127.0.2.12:8000 weight 1 maxconn 1024 check

/etc/nginx/sites-enabled/www.chat.nit

server {
    listen   127.0.1.12:80;

    root /data/node/chat;
    index client.html;

    server_name www.chat.nit;

    # favicon.ico is in /images
    location = /favicon.ico$ { rewrite /(.*) /images/$1 last; }

    # standard includes
    location ^~ /(css|images|scripts)/ {
            try_files $uri =404;
    }

    # html page (only in root dir)
    location ~ ^/([-_a-z]+).html$ {
            try_files $uri =404;
    }

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/www;
    }
}

chat (node.js): server.js

var app = require('http').createServer()
   , io = require('socket.io').listen(app);    
app.listen(8000,'127.0.2.12');

io.sockets.on('connection', function(socket) {
  ...
};

chat: client.html

<head>
  <script src="/scripts/socket.io/socket.io.js"></script>
  <script>
    var socket = io.connect('http://www.chat.nit:80'); 
    ...
  </script>
</head>

notes:

  • link socket.io client js to the directory scripts/

    /.../scripts $ ln -s ../ node_modules / socket.io / node_modules / socket.io-client / dist / socket.io

  • / etc / default / haproxy (unlike text, should work at all)

    Enabled = 1

  • This version of haproxy is not registered. found kvz write about how to use it rsyslogdthrough 127.0.0.1, but could not make it fly.

  • This solution works, not sysadmin quality. (improvements are more than welcome.)

+5
source share
2 answers

( ) HAProxy config WebSockets HTTP-.

global
    maxconn 4096
    nbproc 2
    daemon
    user nobody

defaults
    mode http

frontend app
    bind 0.0.0.0:8000
    mode tcp
    timeout client 86400000
    default_backend www_backend
    acl is_websocket path_beg /sockets

    use_backend socket_backend if is_websocket
    tcp-request inspect-delay 500ms
    tcp-request content accept if HTTP

backend www_backend
    balance roundrobin
    option forwardfor
    mode http
    option httplog
    option httpclose
    timeout server 30000
    timeout connect 4000
    server w1 localhost:8080 weight 1 maxconn 1024 check

backend socket_backend
    balance roundrobin
    mode http
    option httplog
    option forwardfor
    timeout queue 5000
    timeout server 86400000
    timeout connect 86400000
    timeout check 1s
    no option httpclose
    option http-server-close
    option forceclose
    server s1 localhost:8081 weight 1 maxconn 1024 check

, , WS , ( acl is_websocket path_beg /sockets). , , :

acl is_websocket hdr(Upgrade) -i WebSocket

:

acl is_websocket hdr_beg(Host) -i ws

, . nginx .

+2
+4

All Articles