【发布时间】:2014-02-16 09:46:06
【问题描述】:
我正在为多个 Faye 聊天服务器进行 nginx 负载平衡。 我能够在正常的 http 请求上看到显着的性能。但是,与没有 nginx 的结果相比,websocket 连接性能非常低。
这是我的 nginx 配置。
upstream backend {
server 127.0.0.1:4000;
server 127.0.0.1:4002;
server 127.0.0.1:4003;
server 127.0.0.1:4004;
}
server {
listen 4001;
root /var/www/html/laughing-robot;
index index.html index.htm;
server_name backend;
location /faye {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location / {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_redirect off;
proxy_pass http://backend;
}
}
我正在使用websocket-bench 对 Faye 连接(websocket)进行基准测试。
这是没有 nginx 的结果:
user@machine:/etc/nginx/sites-enabled$ websocket-bench -a 2000 -c 500 -t faye http://127.0.0.1:4000/faye
Launch bench with 2000 total connection, 500 concurent connection
0 message(s) send by client
1 worker(s)
WS server : faye
trying : 500 ...
trying : 1000 ...
trying : 1500 ...
trying : 2000 ...
#### steps report ####
┌────────┬─────────────┬────────┬──────────────┐
│ Number │ Connections │ Errors │ Duration(ms) │
├────────┼─────────────┼────────┼──────────────┤
│ 500 │ 500 │ 0 │ 2488 │
├────────┼─────────────┼────────┼──────────────┤
│ 1000 │ 500 │ 0 │ 2830 │
├────────┼─────────────┼────────┼──────────────┤
│ 1500 │ 500 │ 0 │ 2769 │
├────────┼─────────────┼────────┼──────────────┤
│ 2000 │ 500 │ 0 │ 2144 │
└────────┴─────────────┴────────┴──────────────┘
#### total report ####
┌────────┬─────────────┬────────┬──────────────┬──────────────┬──────────────┐
│ Number │ Connections │ Errors │ Message Send │ Message Fail │ Duration(ms) │
├────────┼─────────────┼────────┼──────────────┼──────────────┼──────────────┤
│ 2000 │ 2000 │ 0 │ 0 │ 0 │ 5150 │
└────────┴─────────────┴────────┴──────────────┴──────────────┴──────────────┘
总时长小于6000 ms。
这是使用 nginx 负载均衡器的结果:
user@machine:/etc/nginx/sites-enabled$ websocket-bench -a 2000 -c 500 -t faye http://127.0.0.1:4001/faye
Launch bench with 2000 total connection, 500 concurent connection
0 message(s) send by client
1 worker(s)
WS server : faye
trying : 500 ...
trying : 1000 ...
trying : 1500 ...
trying : 2000 ...
#### steps report ####
┌────────┬─────────────┬────────┬──────────────┐
│ Number │ Connections │ Errors │ Duration(ms) │
├────────┼─────────────┼────────┼──────────────┤
│ 500 │ 500 │ 0 │ 6452 │
├────────┼─────────────┼────────┼──────────────┤
│ 1000 │ 500 │ 0 │ 9394 │
├────────┼─────────────┼────────┼──────────────┤
│ 1500 │ 500 │ 0 │ 12772 │
├────────┼─────────────┼────────┼──────────────┤
│ 2000 │ 500 │ 0 │ 16163 │
└────────┴─────────────┴────────┴──────────────┘
#### total report ####
┌────────┬─────────────┬────────┬──────────────┬──────────────┬──────────────┐
│ Number │ Connections │ Errors │ Message Send │ Message Fail │ Duration(ms) │
├────────┼─────────────┼────────┼──────────────┼──────────────┼──────────────┤
│ 2000 │ 2000 │ 0 │ 0 │ 0 │ 19173 │
└────────┴─────────────┴────────┴──────────────┴──────────────┴──────────────┘
对于总共 2000 个连接和 500 个并发连接,nginx 负载均衡器的性能非常低。
我也配置了nofile & file-max:
/etc/security/limits.conf
* soft nofile 2048
* hard nofile 65536
/etc/sysctl.conf
fs.file-max = 100000
在 Fedora 上,/var/log/nginx/error.log 出现很多连接被拒绝错误。但在 Ubuntu 13.04 上没有错误。
如果有人能够让我朝着正确的方向前进,那将是非常可观的。
谢谢!
【问题讨论】:
-
我看不到你在哪里定义
$connection_upgrade变量。还有你在 nginx 上用什么版本的? -
@alexeyten:它在
/etc/nginx/nginx.conf文件中定义。 -
在我看来
websocket-bench刚刚坏掉了。 -
@VBart:你为什么会这样?你能解释更多吗?
-
即使没有 nginx,它显示的数字也是荒谬的。在 localhost 上打开 2000 个连接没什么大不了的,它不应该超过 10 毫秒。事实上,nginx 可以毫无问题地提供数十万个连接。
标签: node.js nginx benchmarking faye