Files
vps_setup/aa_nginx_optimize.sh
T
2026-08-20 11:15:26 +07:00

257 lines
8.6 KiB
Bash

#!/bin/bash
# Kiểm tra quyền root
if [ "$EUID" -ne 0 ]; then
echo "Vui lòng chạy script bằng quyền root (sudo)."
exit 1
fi
echo "=========================================================="
echo " SỬA LỖI TRÙNG LẶP & TỐI ƯU CẤU TRÚC GỐC AAPANEL "
echo "=========================================================="
# ---------------------------------------------------------
# Bước 1: Đọc thông số phần cứng & Tính toán
# ---------------------------------------------------------
CPU_CORES=$(nproc)
TOTAL_RAM_MB=$(free -m | awk '/^Mem:/{print $2}')
if [ "$TOTAL_RAM_MB" -gt 8000 ]; then
WORKER_CONNECTIONS=65535
SOMAXCONN=65535
MAX_SYN_BACKLOG=65535
CONNTRACK_MAX=262144
else
WORKER_CONNECTIONS=20480
SOMAXCONN=16384
MAX_SYN_BACKLOG=16384
CONNTRACK_MAX=65536
fi
WORKER_LIMIT_NOFILE=$((WORKER_CONNECTIONS * 2))
NGINX_CONF="/www/server/nginx/conf/nginx.conf"
PROXY_CONF="/www/server/nginx/conf/proxy.conf"
if [ ! -f "$NGINX_CONF" ]; then
echo "[-] Không tìm thấy file $NGINX_CONF. Script này chỉ dành cho aaPanel."
exit 1
fi
echo "--- Thông số cấu hình tối ưu sẽ áp dụng ---"
echo "CPU Cores: $CPU_CORES | RAM: $TOTAL_RAM_MB MB"
echo "Worker Connections: $WORKER_CONNECTIONS"
echo "Worker Limit Nofile: $WORKER_LIMIT_NOFILE"
echo "------------------------------------"
# ---------------------------------------------------------
# Bước 2: Tối ưu Kernel mạng (/etc/sysctl.conf)
# ---------------------------------------------------------
echo "[+] Đang cấu hình và tối ưu hàng đợi mạng (sysctl)..."
BACKUP_SYSCTL="/etc/sysctl.conf.bak.$(date +%F_%T)"
cp /etc/sysctl.conf "$BACKUP_SYSCTL"
sed -i '/fs.file-max/d' /etc/sysctl.conf
sed -i '/net.core.somaxconn/d' /etc/sysctl.conf
sed -i '/net.core.netdev_max_backlog/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_syn_backlog/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_fin_timeout/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_tw_reuse/d' /etc/sysctl.conf
sed -i '/net.ipv4.ip_local_port_range/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_max_tw_buckets/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_fastopen/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_rmem/d' /etc/sysctl.conf
sed -i '/net.ipv4.tcp_wmem/d' /etc/sysctl.conf
sed -i '/net.netfilter.nf_conntrack_max/d' /etc/sysctl.conf
cat <<EOF >> /etc/sysctl.conf
# --- ULTRA PROXY OPTIMIZATION ---
fs.file-max = 2097152
net.core.somaxconn = $SOMAXCONN
net.core.netdev_max_backlog = $SOMAXCONN
net.ipv4.tcp_max_syn_backlog = $MAX_SYN_BACKLOG
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_tw_buckets = 262144
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_fastopen = 3
net.netfilter.nf_conntrack_max = $CONNTRACK_MAX
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
EOF
sysctl -p &>/dev/null
# ---------------------------------------------------------
# Bước 3: Tăng giới hạn mở file hệ thống (/etc/security/limits.conf)
# ---------------------------------------------------------
echo "[+] Đang mở rộng giới hạn File Descriptors (uLimit)..."
BACKUP_LIMITS="/etc/security/limits.conf.bak.$(date +%F_%T)"
cp /etc/security/limits.conf "$BACKUP_LIMITS"
sed -i "/www/d" /etc/security/limits.conf
sed -i "/root/d" /etc/security/limits.conf
cat <<EOF >> /etc/security/limits.conf
www soft nofile $WORKER_LIMIT_NOFILE
www hard nofile $WORKER_LIMIT_NOFILE
root soft nofile $WORKER_LIMIT_NOFILE
root hard nofile $WORKER_LIMIT_NOFILE
EOF
# ---------------------------------------------------------
# Bước 4: Sao lưu và Tối ưu hóa file bổ trợ proxy.conf của aaPanel
# ---------------------------------------------------------
if [ -f "$PROXY_CONF" ]; then
BACKUP_PROXY="${PROXY_CONF}.bak.$(date +%F_%T)"
cp "$PROXY_CONF" "$BACKUP_PROXY"
echo "[+] Đã sao lưu và tối ưu lại tệp bổ trợ proxy.conf của aaPanel..."
# Ghi đè file proxy.conf với thông số Buffer tối ưu chịu tải cao
cat <<EOF > "$PROXY_CONF"
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffer_size 64k;
proxy_buffers 16 128k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
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;
proxy_set_header Connection "";
proxy_http_version 1.1;
EOF
fi
# ---------------------------------------------------------
# Bước 5: Khởi tạo nginx.conf (Đã lược bỏ directive trùng)
# ---------------------------------------------------------
echo "[+] Đang cập nhật tệp cấu hình Nginx gốc của aaPanel..."
BACKUP_NGINX="${NGINX_CONF}.bak.$(date +%F_%T)"
cp "$NGINX_CONF" "$BACKUP_NGINX"
cat <<EOF > "$NGINX_CONF"
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile $WORKER_LIMIT_NOFILE;
stream {
log_format tcp_format '\$time_local|\$remote_addr|\$protocol|\$status|\$bytes_sent|\$bytes_received|\$session_time|\$upstream_addr|\$upstream_bytes_sent|\$upstream_bytes_received|\$upstream_connect_time';
access_log /www/wwwlogs/tcp-access.log tcp_format;
error_log /www/wwwlogs/tcp-error.log;
include /www/server/panel/vhost/nginx/tcp/*.conf;
}
events
{
use epoll;
worker_connections $WORKER_CONNECTIONS;
multi_accept on;
}
http
{
include mime.types;
#include luawaf.conf;
include proxy.conf;
#lua_package_path "/www/server/nginx/lib/lua/?.lua;;";
default_type application/octet-stream;
server_names_hash_bucket_size 1024;
# Tối ưu Buffer Header (Không trùng với proxy.conf)
client_header_buffer_size 64k;
large_client_header_buffers 4 32k;
client_max_body_size 256m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 90;
keepalive_requests 10000;
# Cấu hình PHP FastCGI giữ nguyên gốc aaPanel
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
# Gzip chuẩn tốc độ cao
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/json image/jpeg image/gif image/png font/ttf font/otf image/svg+xml application/xml+rss text/x-js;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone \$binary_remote_addr zone=perip:10m;
limit_conn_zone \$server_name zone=perserver:10m;
server_tokens off;
access_log off;
include cloudflare.conf;
server
{
listen 888;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
#error_page 404 /404.html;
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)\$
{
expires 30d;
}
location ~ .*\.(js|css)?\$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}
EOF
echo "[+] Cấu hình Nginx tối ưu hoàn tất."
# ---------------------------------------------------------
# Bước 6: Kiểm tra và Khởi chạy
# ---------------------------------------------------------
echo "[+] Đang kiểm tra cấu hình và kích hoạt..."
/www/server/nginx/sbin/nginx -t
if [ $? -eq 0 ]; then
/etc/init.d/nginx restart
echo "=========================================================="
echo " THÀNH CÔNG! HỆ THỐNG ĐÃ HOẠT ĐỘNG VỚI HIỆU NĂNG ĐỈNH CAO! "
echo "=========================================================="
else
echo "[-] Cấu hình vẫn lỗi! Đang tự động khôi phục toàn bộ trạng thái gốc..."
cp "$BACKUP_NGINX" "$NGINX_CONF"
[ -f "$PROXY_CONF" ] && cp "$BACKUP_PROXY" "$PROXY_CONF"
cp "$BACKUP_SYSCTL" /etc/sysctl.conf && sysctl -p &>/dev/null
cp "$BACKUP_LIMITS" /etc/security/limits.conf
/etc/init.d/nginx restart
exit 1
fi