【问题标题】:nginx nodejs faye performance issuenginx nodejs faye 性能问题
【发布时间】: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


【解决方案1】:

你知道https://github.com/observing/balancerbattle 吗?

如果你在 ubuntu 上没有收到错误,那里的性能如何? (希望这些系统在性能上具有可比性)

不管怎样,看看内核调优部分,你也可以试试他们在测试中使用的 nginx.conf 看看是否会产生相同的结果。

如果可能,您还应该尝试多个负载测试。您所做的测试是在运行 nginx 的服务器上进行的,这些测试如何与您域上的实际外部 ip 进行比较?

我建议在开发机器上而不是在实际服务器上运行负载测试。 另外,对于 nginx 和节点进程,top 对 cpu/memory 的看法是什么? nginx 可能会饿死您的某个进程和/或测试本身吗?

Websockets 比 ssl 更稳定,也值得尝试一下。

他们使用 thor 进行测试,结果是否相同? https://github.com/observing/balancerbattle/blob/master/results/messaging/HAproxy/1k.md

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2012-11-07
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多