2011-08-22
nginx_tcp_proxy_module を入れてnode.jsのsocket.ioを動かすnginxの設定
設定をメモ
node.js の、listenするあたりはこうしてみた。
// node.js(express)はunixソケットをlistenさせてみる。 var app = module.exports = express.createServer(); var sockPath = './tmp/sockets/express.sock'; app.listen(sockPath, function() { fs.chmodSync(sockPath, '777'); }); // socket.io はポート3001をlistenさせる。 var io = require('socket.io').listen(3001),
クライアントサイドで、socket.ioにつなぐコード
// 実際にはポート3001で動かしてるけど、 // 別のポート(3000)でふつうにnginxにリクエストして、 // tcp_poxy_moduleがsocket.ioの3001へプロキシする。 // こうしないと動かなかった var socket = io.connect('http://example.com:3000'); socket.on('hey hey hey hey', function(data) { console.log(data); });
nginxの設定
http {
# 本当はhttpディレクティブのなかは他の設定もいろいろあるけど省略
upstream node-js-myapp {
server unix:/var/apps/myapp/current/tmp/sockets/express.sock;
}
server {
listen 80;
server_name example.com;
root /var/www/myapp;
location / {
try_files $uri @node-js-myapp;
}
location ^~ /socket.io/ {
try_files $uri @node-js-myapp;
}
location /websockets_status {
check_status;
}
location @node-js-myapp {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://node-js-myapp;
}
}
}
tcp {
upstream websocket-myapp {
server 127.0.0.1:3001;
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
listen 3000;
server_name example.com;
proxy_read_timeout 200000;
proxy_send_timeout 200000;
proxy_pass websocket-myapp;
}
}
トラックバック - http://d.hatena.ne.jp/hadashia/20110822/1314017835
リンク元
- 2 http://docomo.ne.jp/cp/as-rslt.cgi?pno=1&key=解読してくれる人&sid=000
- 2 http://www.google.co.jp/search?cx=w&sourceid=chrome&ie=UTF-8&q=nginx_tcp_proxy_module
- 1 http://d.hatena.ne.jp/keyword/nginx
- 1 http://d.hatena.ne.jp/keywordstats/Node.js
- 1 http://d.hatena.ne.jp/waritohutsu/20110823/1314117563
- 1 http://d.hatena.ne.jp/waritohutsu/20110823/1314123070
- 1 http://ezsch.ezweb.ne.jp/search/?query=サティスファクション+まんべ&ct=0001&pd=1&sr=0000
- 1 http://ezsch.ezweb.ne.jp/search/?sr=0101&query=自分 嫌い 考え
- 1 http://ezsch.ezweb.ne.jp/search/?sr=0101&query=難しい単語をTシャツに書いて
- 1 http://k.hatena.ne.jp/keywordblog/Node.js?date=20110822
