Mục đích chung của việc này là giúp Nginx hoạt động hiệu quả hơn, sử dụng tài nguyên hợp lý hơn, thông qua đó tăng cường khả năng chịu tải của toàn bộ hệ thống, không lãng phí tài nguyên vô ích.

Tối ưu Nginx

Ở bước này, chúng ta sẽ cùng sửa lại tập tin nginx.conf mặc định trên VPS vì có một số tùy chọn nó sẽ trở nên tốt hơn nếu bạn biết cách cấu hình lại. Tập tin nginx.conf này sẽ nằm ở thư mục /etc/nginx/ nếu các bạn làm theo series này.

Cấu hình Nginx

user nginx;
worker_processes auto;
worker_rlimit_nofile 100000;

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

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

http {
    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;

    open_file_cache max=200000 inactive=20s; 
    open_file_cache_valid 30s; 
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 60;
    keepalive_requests 100000;
    reset_timedout_connection on;

    gzip on;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    gzip_static on;
    gzip_min_length 1400;
    gzip_buffers 32 8k;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml application/javascript application/x-javascript application/xml application/xml+rss application/ecmascript application/json image/svg+xml;

    client_body_buffer_size 256k;
    client_body_in_file_only off;
    client_body_timeout 12;
    client_header_timeout 12;
    send_timeout 10;
    client_header_buffer_size 64k;
    large_client_header_buffers 4 256k;
    client_max_body_size 32m;
    connection_pool_size 512;

    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    fastcgi_buffering on;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

    include /etc/nginx/conf.d/*.conf;
}

Sau khi cập nhật, khởi động lại Nginx:

systemctl restart nginx.service

Chúc các bạn thành công!