Khi chạy Nginx trên server có 12GB RAM và 6 CPU core, việc tối ưu worker, buffer, gzip và cache giúp tăng hiệu năng, giảm latency và tận dụng tối đa phần cứng. Bài viết này sẽ hướng dẫn cách cấu hình Nginx hiệu quả mà vẫn giữ nguyên cấu trúc cơ bản.
File chính của Nginx thường nằm tại:
/etc/nginx/nginx.conf
Bạn nên sao lưu file cũ trước:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
Dưới đây là file cấu hình đã tối ưu cho 12GB RAM – 6 core:
user www-data; worker_processes auto; worker_rlimit_nofile 100000; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 8192; multi_accept on; use epoll; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 10000; types_hash_max_size 2048; server_tokens off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Compression ## gzip on; gzip_comp_level 5; gzip_min_length 1024; gzip_proxied any; gzip_vary on; gzip_types text/plain text/css application/json application/javascript application/xml application/xml+rss text/javascript image/svg+xml; ## # Buffers & Timeouts ## client_max_body_size 50M; client_body_buffer_size 256k; client_header_buffer_size 1k; large_client_header_buffers 4 32k; output_buffers 2 512k; postpone_output 1460; ## # Open File Cache ## open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; ## # Virtual Hosts ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
sudo nginx -t
sudo systemctl reload nginx
worker_connections
hoặc open_file_cache
để phù hợp./etc/nginx/sites-enabled/
để dễ quản lý.👉 Với cấu hình trên, Nginx của bạn sẽ tận dụng tối đa 12GB RAM và 6 core CPU, xử lý hàng chục nghìn kết nối đồng thời mà vẫn ổn định.