[英]nginx, node.js and socket.io - is there a working marriage?
nginx
is a killer static file server. nginx
是一个杀手级静态文件服务器。
it can serve node.js
, as in this example , but in a limited fashion. 如本例中所示 ,它可以服务于
node.js
,但方式有限。
but nginx
is apparently unable to proxy websockets
. 但是
nginx
显然无法代理websockets
。
the only thing I found that might work is using HAProxy front end as per this article - but it's from October 6, 2011. 我唯一发现可能的工作是使用HAProxy的前端按照这篇文章 -但它从2011年10月6日,是。
this has to be a common problem, but I'm not finding a very common solution. 这必须是一个常见的问题,但我没有找到一个非常普遍的解决方案。
(see https://github.com/bangkok-maco/barebone-node for complete solution and details) (有关完整的解决方案和详细信息,请参见https://github.com/bangkok-maco/barebone-node )
ip testing schema: ip测试架构:
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
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;
}
}
var app = require('http').createServer()
, io = require('socket.io').listen(app);
app.listen(8000,'127.0.2.12');
io.sockets.on('connection', function(socket) {
...
};
<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 into scripts/
directory 将socket.io客户端js链接到
scripts/
目录
/.../scripts$ ln -s ../node_modules/socket.io/node_modules/socket.io-client/dist/ socket.io /.../脚本$ ln -s ../node_modules/socket.io/node_modules/socket.io-client/dist/ socket.io
/etc/default/haproxy (contrary to text, must set to work at all ) 在/ etc /默认/ HAProxy的(与文本,必须设置为工作在所有 )
ENABLED=1 已启用= 1
this version haproxy not logging. 此版本haproxy不记录。 found kvz's write up on how to use
rsyslogd
via 127.0.0.1, but could not make it fly. 找到了kvz关于如何通过127.0.0.1使用
rsyslogd
,但是无法使其运行。
this solution is working - not sysadmin quality to be sure. 此解决方案有效 -不能确定sysadmin的质量。 (enhancements more than welcome.)
(增强功能不只是欢迎功能。)
It looks like that you can proxy WebSockets through nginx since v1.3.13 自v1.3.13以来,您似乎可以通过nginx代理WebSockets
See http://nginx.org/en/docs/http/websocket.html for more details 有关更多详细信息,请参见http://nginx.org/en/docs/http/websocket.html
Here's my (old and for testing purposes) HAProxy config for proxying WebSockets and normal HTTP requests. 这是我的(用于测试目的的旧版本)HAProxy配置,用于代理WebSocket和常规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
Note that I am recognizing whether request is WS or not by looking at path ( acl is_websocket path_beg /sockets
line). 请注意,我通过查看路径(
acl is_websocket path_beg /sockets
行)来识别请求是否为WS。 This can be replaced with for example this: 可以替换为例如:
acl is_websocket hdr(Upgrade) -i WebSocket
or this: 或这个:
acl is_websocket hdr_beg(Host) -i ws
or both. 或两者。 Proxying to nginx with this config should work out of the box.
使用此配置代理到nginx应该可以立即使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.