- Mình nghĩ trước hết bạn nên kiểm tra xem bạn đã cấu hình tốt nginx để serve static files và cache cho node app của bạn chưa.
Demo nginx config mà mình vẫn dùng...
upstream DOMAIN {
server 127.0.0.1:3000;
keepalive 64;
}
# Init cache
proxy_cache_path /var/run/cache levels=1:2 keys_zone=DOMAIN_CACHE:75m inactive=24h max_size=512m;
server {
listen YOUR.SERVER.IP.ADDRESS:80;
server_name your.domain www.your.domain;
error_log /path/to/your/domains/your.domain.error.log error;
# Pages
location / {
proxy_cache DOMAIN_CACHE;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 10m;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
proxy_hide_header X-powered-by;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
expires 10m;
proxy_pass http://DOMAIN;
add_header X-Cache $upstream_cache_status;
}
# Static files
location ~* \.(jpg|jpeg|svg|png|gif|ico|css|js|eot|woff)$ {
# Use the nginx cache zone called APP
proxy_cache DOMAIN_CACHE;
# For valid responses, cache it for 1 day
proxy_cache_valid 200 1d;
# For not found, cache it for 10 minutes
proxy_cache_valid 404 10m;
# Sends Cache-Control max-age=0 on CSS/JS for now
proxy_ignore_headers "Cache-Control";
access_log off;
# Allow the browser to cache static files for 30 days
expires 30d;
proxy_pass http://DOMAIN;
}
# Static images
location /content/images {
alias /path/to/your/domain/public_html/content/images;
access_log off;
expires max;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
}
- Đó là trong trường hợp site của bạn nhận tương đối nhiều request mỗi giây. Còn nếu không có hoặc rất ít request thì bạn cần xem lại code của mình rồi.
- Trong trường hợp bạn keep app running với
pm2
, nodemon
hay forever
thì cần chú ý nếu code bị lỗi thì mấy thằng này sẽ restart app của bạn liên tục. Và quá trình khởi động app sẽ khá là ngốn chip.
- Nếu vẫn không phải thì kiểm tra các vòng lặp trong code, các query quá nặng (kéo quá nhiều dữ liệu). Mình từng gặp trường hợp là query và lấy ra object quá lớn và nhiều cấp, cũng gây ngốn RAM và chip.
- Và nếu vẫn không phải luôn thì bạn nên cung cấp log cho mọi người xem để biết chính xác hơn vấn đề bạn đang gặp phải.