Nginx เป็น Web Server ที่มีประสิทธิภาพสูงและใช้ทรัพยากรระบบน้อย หากต้องการให้ Nginx ทำงานด้วยความเร็วสูงสุดบน Cloud VPS คุณต้องปรับแต่งการตั้งค่าทั้งด้านไฟล์โครงร่างและด้านระบบ Linux บทความนี้จะแนะนำวิธีการปรับแต่ง Nginx ให้เหมาะสมกับความต้องการของคุณ
ตรวจสอบเวอร์ชัน Nginx และหมายลักษณ์ Module
# ดูเวอร์ชัน Nginx
ngix -v
# ดูรายละเอียด Module ที่ Compile มาด้วย
nginx -V
# ตรวจสอบไฟล์โครงร่าง Nginx หลัก
ls -la /etc/nginx/nginx.conf
# ดูการตั้งค่าปัจจุบัน
sudo nginx -t
ปรับแต่งไฟล์ nginx.conf หลัก
เปิดไฟล์โครงร่าง Nginx หลักและปรับแต่งพารามิเตอร์สำคัญ:
sudo nano /etc/nginx/nginx.conf
ค้นหาและปรับแต่งส่วน worker_processes และ worker_connections:
# กำหนด Worker Processes ตามจำนวน CPU Core
worker_processes auto; # ใช้ auto เพื่อให้ Nginx ตรวจจับโดยอัตโนมัติ
# หรือระบุจำนวนที่ชัดแจ้ง (ตัวอย่าง VPS 4 core)
worker_processes 4;
# กำหนด Max Connections ต่อ Worker
events {
worker_connections 10000; # เพิ่มจาก 1024 เป็น 10000
use epoll; # ใช้ epoll สำหรับ Linux
multi_accept on; # รับหลาย Connection พร้อมกัน
}
# ปรับแต่ง Buffer Sizes
http {
# Buffer sizes สำหรับ Upstream Servers
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
# Client Upload Size Limit
client_max_body_size 100M;
# Gzip Compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss
application/json application/javascript;
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
client_body_timeout 60s;
client_header_timeout 60s;
keepalive_timeout 75s;
}
ปรับแต่งการตั้งค่า Server Block
สำหรับแต่ละ Virtual Host ให้ปรับแต่งดังนี้:
server {
listen 80;
server_name example.com www.example.com;
# Enable Caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Proxy Settings (สำหรับ Reverse Proxy)
location / {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Connection Pooling
proxy_http_version 1.1;
proxy_set_header Connection "";
}
# Disable Access Logging สำหรับ Static Files
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
access_log off;
}
# Rate Limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
location /api/ {
limit_req zone=api burst=20 nodelay;
}
}
ปรับแต่งระดับระบบ Linux
นอกจากไฟล์โครงร่าง Nginx แล้ว ยังต้องปรับแต่งพารามิเตอร์ Linux ของระบบ:
# ดู Limits ปัจจุบัน
ulimit -n
# เพิ่มจำนวน File Descriptors
sudo nano /etc/security/limits.conf
# เพิ่มบรรทัดต่อไปนี้
www-data soft nofile 65536
www-data hard nofile 65536
nginx soft nofile 65536
nginx hard nofile 65536
# ปรับแต่ง Kernel Parameters
sudo nano /etc/sysctl.conf
# เพิ่มหรือแก้ไขค่าต่อไปนี้
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_fin_timeout = 30
net.core.somaxconn = 4096
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
# ใช้ค่าใหม่
sudo sysctl -p
ตรวจสอบความถูกต้องและ Reload Nginx
# ทดสอบไฟล์โครงร่าง
sudo nginx -t
# Reload Nginx โดยไม่ขัดจังหวะบริการ
sudo systemctl reload nginx
# หรือ
sudo nginx -s reload
# ตรวจสอบสถานะ
sudo systemctl status nginx
ตรวจสอบประสิทธิภาพหลังการปรับแต่ง
หลังจากปรับแต่งแล้ว ให้ตรวจสอบประสิทธิภาพด้วยเครื่องมือต่าง ๆ:
# ดู Process Nginx ทั้งหมด
ps aux | grep nginx
# ตรวจสอบ CPU และ Memory Usage
top -p $(pgrep -f nginx | tr '\n' ',')
# ดู Network Connection
ss -tlnp | grep nginx
netstat -an | grep ESTABLISHED | wc -l
# ใช้ ab (Apache Bench) สำหรับ Load Testing
ab -n 10000 -c 100 http://example.com/
# หรือใช้ wrk สำหรับ Load Testing ขั้นสูง
wrk -t4 -c100 -d30s http://example.com/
เรียงลำดับ Configuration ที่แนะนำ
# ตัวอย่าง Production-Ready nginx.conf
user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10000;
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;
error_log /var/log/nginx/error.log warn;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75s;
types_hash_max_size 2048;
client_max_body_size 100M;
# Gzip Compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss application/json;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
สรุป
การปรับแต่ง Nginx เพื่อให้ได้ประสิทธิภาพสูงสุดนั้นเกี่ยวข้องกับหลายด้าน ตั้งแต่การตั้งค่าไฟล์โครงร่าง Nginx เอง การปรับแต่งระดับระบบ Linux จนถึงการใช้เครื่องมือสำหรับทดสอบประสิทธิภาพ หากคุณใช้ Cloud VPS จาก Dot Enterprise คุณสามารถใช้คำแนะนำดังกล่าวได้ทันทีเพื่อปรับแต่ง Nginx ให้มีประสิทธิภาพสูงสุด
- Systemd Service Management บน Linux VPS — คู่มือครบจบ
- ติดตั้ง WireGuard VPN บน Cloud VPS — คู่มือฉบับสมบูรณ์
- ตั้งค่า UFW Firewall และ Fail2ban บน VPS — ป้องกันการบุกรุกอย่างมีประสิทธิภาพ
- SSH Hardening บน VPS — เสริมความปลอดภัยด้วย Command Line
- ติดตั้ง Redis Cache บน Cloud VPS — เพิ่มความเร็ว Application
