nodejs 与 nginx 代理,内存使用情况

nodejs 与 nginx 代理,内存使用情况

我使用 nginx 代理到 nodejs 应用程序(express),我发现内存使用量比其他 php 应用程序大,我是 nodejs 新手,请帮我检查一下这些配置和状态是否正常或者有什么问题?

# ps aux --sort -rss | head

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      3587  0.1  9.3 1122596 191268 ?      Ssl  Oct10   0:47 node /var/www/html/domain1.com/app/app.js
mongod    2266  0.4  2.6 652476 53572 ?        Sl   Oct01  65:18 /usr/bin/mongod -f /etc/mongod.conf
root      2380  0.0  1.9 984316 39184 ?        Ssl  Oct01   8:18 PM2 v0.14.7: God Daemon
nginx   18011  6.2  0.9 253192 18576 ?        S    02:56   0:00 php-fpm: pool www
nginx   17492  3.0  0.9 252996 18452 ?        S    02:53   0:05 php-fpm: pool www
nginx   17269  3.1  0.9 252984 18428 ?        S    02:52   0:07 php-fpm: pool www
nginx   17261  3.1  0.9 252888 18380 ?        S    02:51   0:08 php-fpm: pool www
nginx   17201  3.3  0.8 252936 18148 ?        S    02:51   0:10 php-fpm: pool www
postgres  3596  0.0  0.5 326276 11844 ?        Ss   Oct10   0:01 postgres: db_admin db 127.0.0.1(54307) idle

# free -m

             total       used       free     shared    buffers     cached
Mem:          1991       1209        781          8        121        221
-/+ buffers/cache:        866       1124 
Swap:          511         48        463 

nginx 配置

server {
  listen 80;
  server_name domain1.com  www.domain1.com;
  access_log /var/log/nginx/domain1.com.access.log;
  location ~ ^/sitemap/(.*)$ {
    root /var/www/html/domain1.com/app/Sitemap/SitemapGz;
  }
  location /robots.txt {
    alias /var/www/html/domain1.com/app/robots.txt;
  }
  location ~ ^/(images/|javascripts/|stylesheets/|fonts) {
    root /var/www/html/domain1.com/app/assets;
    access_log off;
    expires max;
  }
  location / {
    set $fixed_destination $http_destination;
    if ( $http_destination ~* ^https(.*)$ )
    {
    set $fixed_destination http$1;
    }
    proxy_pass http://127.00.0.1:8002/;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Destination $fixed_destination;

    client_max_body_size 32M;
    client_body_buffer_size 512k;
    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
  }
}

nginx 配置

user              nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;


events {
    worker_connections  2048;
}


http {
  # fastcgi cache ...


  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  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;

  #keepalive_timeout  0;
  keepalive_timeout  65;

  reset_timedout_connection on;
  send_timeout 2;

  gzip  on;

  gzip_comp_level    5;
  gzip_min_length    256;
  gzip_proxied       any;
  gzip_vary          on;

  gzip_types
  application/atom+xml
  application/javascript
  application/json
  application/ld+json
  application/manifest+json
  application/rdf+xml
  application/rss+xml
  application/schema+json
  application/vnd.geo+json
  application/vnd.ms-fontobject
  application/x-font-ttf
  application/x-javascript
  application/x-web-app-manifest+json
  application/xhtml+xml
  application/xml
  font/eot
  font/opentype
  image/bmp
  image/svg+xml
  image/vnd.microsoft.icon
  image/x-icon
  text/cache-manifest
  text/css
  text/javascript
  text/plain
  text/vcard
  text/vnd.rim.location.xloc
  text/vtt
  text/x-component
  text/x-cross-domain-policy
  text/xml;


  # Load config files from the /etc/nginx/conf.d directory
  # The default server is in conf.d/default.conf
  include /etc/nginx/conf.d/*.conf;
}

答案1

Node.js 底层使用 javascript V8 引擎,该引擎使用复杂的垃圾收集算法,因此如果它发现您有足够的可用内存,它将尝试利用它。这意味着内存使用率高并不一定表示存在问题 - node.js 可能使用所有可用内存的 85-90%,但它运行速度超快,没有任何问题。高内存使用率可能是正常的,也可能是一个问题,但无法分辨。除非您的应用程序崩溃或运行速度非常慢,否则我不会担心它,但我会密切关注它以建立基线。

顺便说一句,如果你碰巧使用 newrelic 来监控你的应用程序,它往往会使内存使用量增加 2-4 倍,但这就是它的工作方式,没有任何问题。然而,它也有与 SSL 相关的记录内存泄漏,所以你可能需要检查它是否对你有影响。

相关内容