Add aa_nginx_optimize.sh
This commit is contained in:
1 parent
05e5e29d53
commit
fead852c15
1 file changed
+259
@@ -0,0 +1,259 @@
|
||||
#!/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 " BẮT ĐẦU TỐI ƯU HỆ THỐNG THEO 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}')
|
||||
|
||||
# Tính toán các thông số chịu tải dựa trên RAM
|
||||
if [ "$TOTAL_RAM_MB" -gt 8000 ]; then
|
||||
# Hệ thống RAM lớn (> 8GB)
|
||||
WORKER_CONNECTIONS=65535
|
||||
SOMAXCONN=65535
|
||||
MAX_SYN_BACKLOG=65535
|
||||
CONNTRACK_MAX=262144
|
||||
else
|
||||
# Hệ thống RAM nhỏ (<= 8GB)
|
||||
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"
|
||||
|
||||
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 "Kernel Somaxconn: $SOMAXCONN"
|
||||
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"
|
||||
|
||||
# Dọn dẹp cấu hình cũ chống trùng lặp
|
||||
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
|
||||
echo "[+] Cấu hình Kernel mạng hoàn tất."
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# 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
|
||||
echo "[+] Cấu hình giới hạn ulimit hoàn tất."
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Bước 4: Khởi tạo nginx.conf tối ưu dựa trên cấu trúc aaPanel
|
||||
# ---------------------------------------------------------
|
||||
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ăng Buffer chống nghẽn Header và Request lớn khi làm Proxy
|
||||
client_header_buffer_size 64k;
|
||||
large_client_header_buffers 4 32k;
|
||||
client_body_buffer_size 128k;
|
||||
client_max_body_size 256m;
|
||||
|
||||
sendfile on;
|
||||
tcp_nopush on;
|
||||
tcp_nodelay on;
|
||||
|
||||
# Keepalive lớn kết hợp chống treo kết nối cũ
|
||||
keepalive_timeout 90;
|
||||
keepalive_requests 10000;
|
||||
|
||||
# Tối ưu hóa các thông số FastCGI cho PHP (nếu có dùng vhost thường)
|
||||
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;
|
||||
|
||||
# Tối ưu hóa cấu hình lõi cho Nginx Reverse Proxy toàn cục
|
||||
proxy_http_version 1.1;
|
||||
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 ""; # Kích hoạt Keepalive Upstream liên tục tới backend
|
||||
|
||||
# Tinh chỉnh Proxy Buffer để tránh thắt nút cổ chai Disk I/O
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
proxy_buffering on;
|
||||
proxy_buffer_size 64k;
|
||||
proxy_buffers 16 128k;
|
||||
proxy_busy_buffers_size 256k;
|
||||
proxy_temp_file_write_size 256k;
|
||||
|
||||
# Gzip chuẩn chịu tải và tối ưu dung lượng truyền tải
|
||||
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]\.";
|
||||
|
||||
# Giới hạn kết nối phòng chống DDoS cơ bản
|
||||
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 5: 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 " HOÀN THÀNH TỐI ƯU CỰC HẠN THEO CẤU TRÚC GỐC AAPANEL! "
|
||||
echo "=========================================================="
|
||||
else
|
||||
echo "[-] Cấu hình lỗi! Đang khôi phục lại trạng thái cũ..."
|
||||
cp "$BACKUP_NGINX" "$NGINX_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
|
||||
Reference in new issue
Block a user