Nginx
nodejs 與 nginx 代理,記憶體使用
我使用 nginx 代理到 nodejs 應用程序(快遞),我發現記憶體使用量比其他 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; }
Node.js 在底層使用 javascript V8 引擎,該引擎使用複雜的垃圾收集算法,所以如果它看到你有足夠的可用記憶體,它會嘗試利用它。這意味著記憶體高記憶體使用並不一定表明存在問題——node.js 可能使用了所有可用記憶體的 85-90%,但它執行得非常快而且沒有任何問題。高記憶體使用可能是正常的,也可能是問題但無法判斷。除非您的應用程序崩潰執行非常緩慢,否則我不會擔心它,但我會密切關注它以建立基線。
順便說一句,如果您有任何機會使用 newrelic 來監控您的應用程序,它往往會導致記憶體使用量增加 2-4 次,但這就是它的工作方式,並且沒有任何問題。但是,它也有與 SSL 相關的記錄記憶體洩漏,因此您可能需要檢查它是否會影響您。