Nginx、Node.js和Faye性能问题

3

我正在为多个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毫秒

以下是使用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负载均衡器性能较低。

我还配置了nofilefile-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版本是什么? - Alexey Ten
@alexeyten:它在/etc/nginx/nginx.conf文件中定义。 - user405398
在我看来,“websocket-bench”似乎是有问题的。 - VBart
@VBart:为什么你会这样想?你能详细解释一下吗? - user405398
即使在没有nginx的情况下,它显示的数字也是荒谬的。在本地主机上打开2000个连接并不是什么大问题,不应该需要超过10毫秒的时间。实际上,nginx可以轻松处理数十万个连接而没有任何问题。 - VBart
1个回答

0

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

如果在Ubuntu上没有出现任何错误,它的性能如何呢? (希望系统的性能是可比较的)

无论如何,看一下内核调优部分,您也可以尝试使用他们在测试中使用的nginx.conf,看看是否会产生相同的结果。

此外,如果可能的话,请尝试多个负载测试。您所做的测试是在运行nginx的服务器上进行的,从实际的外部IP地址通过您的域名,测试结果如何?

我建议在开发机器上运行负载测试,而不是在实际服务器上运行。 另外,对于nginx和节点进程,top命令显示CPU/内存状况如何? Nginx是否可能使其中一个进程或测试本身饥饿?

在ssl上,Websockets更加稳定,值得一试。

他们在测试中使用了Thor,这会给你相同的结果吗? https://github.com/observing/balancerbattle/blob/master/results/messaging/HAproxy/1k.md


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接