Node.js + Nginx - 为什么这么慢?

时间:2016-12-13 00:34:42

标签: node.js nginx webserver reverse-proxy

我正在运行一个简单的ab测试来获取针对我的节点应用的性能指标。

当我直接针对应用运行ab时(在端口10.0.0.5上针对31005运行5次测试),我看到~1100 req/sec的吞吐量:

> ab -q -n 12000 -c 300 -k "http://10.0.0.5:31005" | grep "Requests per second"

Requests per second:    1156.51 [#/sec] (mean)
Requests per second:    1201.85 [#/sec] (mean)
Requests per second:    1238.40 [#/sec] (mean)
Requests per second:    1392.68 [#/sec] (mean)
Requests per second:    1294.58 [#/sec] (mean)

同样,当我在节点应用程序前面放置HAPrxoy(在端口10.0.0.4上运行8888)并运行相同的测试时,我看到非常相似的吞吐量:

> ab -q -n 12000 -c 300 -k "http://10.0.0.4:8888" | grep "Requests per second"

Requests per second:    1179.97 [#/sec] (mean)
Requests per second:    1176.60 [#/sec] (mean)
Requests per second:    1308.40 [#/sec] (mean)
Requests per second:    1294.56 [#/sec] (mean)
Requests per second:    1246.76 [#/sec] (mean)

然而,当我通过Nginx(也在10.0.0.4上运行,但在端口80上)运行时,我看到性能下降了约30%:

> ab -q -n 12000 -c 300 -k "http://10.0.0.4" | grep "Requests per second"

Requests per second:    836.16 [#/sec] (mean)
Requests per second:    803.62 [#/sec] (mean)
Requests per second:    766.50 [#/sec] (mean)
Requests per second:    847.16 [#/sec] (mean)
Requests per second:    852.60 [#/sec] (mean)

我的nginx配置非常简单

worker_processes 2;
worker_rlimit_nofile 40000;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
  worker_connections  40000;
  use epoll;
  multi_accept on;
}

http {

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;

  keepalive_timeout  65;
  keepalive_requests 100000;

  client_body_buffer_size      128k;
  client_max_body_size         10m;
  client_header_buffer_size    1k;
  large_client_header_buffers  4 4k;
  output_buffers               1 32k;
  postpone_output              1460;

  gzip  on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_vary off;
  gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\.";


server {
    listen 80;
    server_name 10.0.0.4;
      access_log  /var/log/nginx/app-access.log  main;
      error_log     /var/log/nginx/app-error.log;


   location / {
       proxy_pass         http://10.0.0.5:31005$request_uri;
    }

}
}

我使用top检查了资源使用情况,但我从未在服务器上看到CPU或内存最大值。我正在使用4 GB内存的双核运行,我会认为足以处理我投入的负载。

此外,ulimit -n会返回40000

你能看出我在Nginx上遇到如此糟糕表现的原因吗?

其他信息:

Nginx版

nginx version: nginx/1.10.0 (Ubuntu)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

0 个答案:

没有答案