-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnginx-tune.sh
More file actions
116 lines (97 loc) · 5.06 KB
/
nginx-tune.sh
File metadata and controls
116 lines (97 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
#----------------------------------------------------------------------------------
# EngineScript - A High-Performance WordPress Server Built on Ubuntu and Cloudflare
#----------------------------------------------------------------------------------
# Website: https://EngineScript.com
# GitHub: https://github.com/Enginescript/EngineScript
# License: GPL v3.0
#----------------------------------------------------------------------------------
# EngineScript Variables
source /usr/local/bin/enginescript/enginescript-variables.txt || { echo "Error: Failed to source /usr/local/bin/enginescript/enginescript-variables.txt" >&2; exit 1; }
source /home/EngineScript/enginescript-install-options.txt || { echo "Error: Failed to source /home/EngineScript/enginescript-install-options.txt" >&2; exit 1; }
# Source shared functions library
source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.sh || { echo "Error: Failed to source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.sh" >&2; exit 1; }
#----------------------------------------------------------------------------------
# Start Main Script
# Tune FastCGI Cache
sed -i "s|SEDSERVERMEM03|${SERVER_MEMORY_TOTAL_03}|g" /etc/nginx/nginx.conf
sed -i "s|SEDSERVERMEM05|${SERVER_MEMORY_TOTAL_05}|g" /etc/nginx/nginx.conf
if [[ "${SERVER_MEMORY_TOTAL_100}" -lt 1400 ]];
then
sed -i "s|SEDFCGIBUFFERS|8 32k|g" /etc/nginx/nginx.conf
else
sed -i "s|SEDFCGIBUFFERS|16 32k|g" /etc/nginx/nginx.conf
fi
if [[ "${SERVER_MEMORY_TOTAL_100}" -lt 1400 ]];
then
sed -i "s|SEDFCGIBUSYBUFFERS|128k|g" /etc/nginx/nginx.conf
else
sed -i "s|SEDFCGIBUSYBUFFERS|256k|g" /etc/nginx/nginx.conf
fi
if [[ "${SERVER_MEMORY_TOTAL_100}" -lt 1400 ]];
then
sed -i "s|SEDFCGITEMPFILEWRITESIZE|128k|g" /etc/nginx/nginx.conf
else
sed -i "s|SEDFCGITEMPFILEWRITESIZE|256k|g" /etc/nginx/nginx.conf
fi
# Tune Nginx Threads and variables_hash_bucket_size
# Note: Nginx Threads tuning not implemented yet
# Get CPU information using lscpu and store it in a variable
CPU_INFO=$(lscpu)
# Extract specific information from the output
CPU_MODEL=$(echo "$CPU_INFO" | grep "Model name:" | awk '{print $3,$4,$5,$6,$7,$8,$9}')
CPU_CORES=$(echo "$CPU_INFO" | grep "CPU(s):" | awk '{print $2}')
CPU_THREADS=$(echo "$CPU_INFO" | grep "Thread(s) per core:" | awk '{print $4}')
CPU_CACHE=$(echo "$CPU_INFO" | grep "L1d cache:" | awk '{print $3}')
# Print the extracted information
echo "CPU Model: $CPU_MODEL"
echo "Number of Cores: $CPU_CORES"
echo "Threads per Core: $CPU_THREADS"
echo "L3 Cache: $CPU_CACHE"
# Calculate variables_hash_bucket_size
# variables_hash_bucket_size should be 2x the CPU level 1 cache value
sed -i "s|SEDHBS|$(lscpu | grep "L1d cache:" | awk '{print $3 * 2}')|g" /etc/nginx/nginx.conf
# Tuning Worker Connections
# Nginx Worker Connections - scaled by RAM tier
if [[ "${SERVER_MEMORY_TOTAL_100}" -lt 1200 ]]; then
sed -i "s|SEDNGINXRLIMIT|1024|g" /etc/nginx/nginx.conf
sed -i "s|SEDNGINXWORKERCONNECTIONS|512|g" /etc/nginx/nginx.conf
elif [[ "${SERVER_MEMORY_TOTAL_100}" -lt 2200 ]]; then
sed -i "s|SEDNGINXRLIMIT|2048|g" /etc/nginx/nginx.conf
sed -i "s|SEDNGINXWORKERCONNECTIONS|1024|g" /etc/nginx/nginx.conf
elif [[ "${SERVER_MEMORY_TOTAL_100}" -lt 4200 ]]; then
sed -i "s|SEDNGINXRLIMIT|8192|g" /etc/nginx/nginx.conf
sed -i "s|SEDNGINXWORKERCONNECTIONS|4096|g" /etc/nginx/nginx.conf
else
sed -i "s|SEDNGINXRLIMIT|10240|g" /etc/nginx/nginx.conf
sed -i "s|SEDNGINXWORKERCONNECTIONS|5120|g" /etc/nginx/nginx.conf
fi
# Hash Bucket Size
NGINX_HASH_BUCKET="$(cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size)"
if [[ "${NGINX_HASH_BUCKET}" = 128 ]];
then
sed -i "s|SEDHASHBUCKETSIZE|128|g" /etc/nginx/nginx.conf
sed -i "s|SEDHASHMAXSIZE|4096|g" /etc/nginx/nginx.conf
else
sed -i "s|SEDHASHBUCKETSIZE|64|g" /etc/nginx/nginx.conf
sed -i "s|SEDHASHMAXSIZE|2048|g" /etc/nginx/nginx.conf
fi
# HTTP3
if [[ "${INSTALL_HTTP3}" = 1 ]]; then
sed -i "s|#http3 on;|http3 on;|g" /etc/nginx/nginx.conf
sed -i "s|#http3_max_concurrent_streams|http3_max_concurrent_streams|g" /etc/nginx/nginx.conf
sed -i "s|#http3_stream_buffer_size|http3_stream_buffer_size|g" /etc/nginx/nginx.conf
sed -i "s|#quic_bpf on|quic_bpf on|g" /etc/nginx/nginx.conf
sed -i "s|#quic_retry on|quic_retry on|g" /etc/nginx/nginx.conf
sed -i "s|#add_header Alt-Svc|add_header Alt-Svc|g" /etc/nginx/globals/response-headers.conf
sed -i "s|#add_header x-quic|add_header x-quic|g" /etc/nginx/globals/response-headers.conf
sed -i "s|#listen 443 quic|listen 443 quic|g" "/etc/nginx/admin/admin.localhost.conf"
sed -i "s|#listen [::]:443 quic|listen [::]:443 quic|g" "/etc/nginx/admin/admin.localhost.conf"
fi
# HTTP3 - QUIC GSO (requires hardware support check)
if [[ "${INSTALL_HTTP3}" = 1 ]] && ethtool -k eth0 | grep "tx-gso-robust: on"; then
sed -i "s|#quic_gso on|quic_gso on|g" /etc/nginx/nginx.conf
fi
# References:
# https://www.cloudbees.com/blog/tuning-nginx
# https://serverfault.com/questions/1153941/does-anyone-have-a-best-practices-guide-for-nginx-with-http3-quic/1172800#1172800