-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx_optimize.sh
More file actions
62 lines (49 loc) · 1.93 KB
/
Copy pathnginx_optimize.sh
File metadata and controls
62 lines (49 loc) · 1.93 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
#!/bin/bash
set -e
CUSTOM_FILNAME=$(basename "$0")
echo "file name with "$CUSTOM_FILNAME
# 定义 nginx.conf 的路径
nginx_conf="/etc/nginx/nginx.conf"
nginx_limit_base=51200
# 更新 nginx.conf 文件
function update_nginx_conf() {
# 备份 nginx 配置文件
cp $nginx_conf "$nginx_conf.bak"
if ! grep -q "worker_rlimit_nofile" $nginx_conf; then
echo "worker_rlimit_nofile ${nginx_limit_base};" >> $nginx_conf
else
sed -i "/worker_rlimit_nofile/c\worker_rlimit_nofile ${nginx_limit_base};" $nginx_conf
fi
if ! grep -q "events {" $nginx_conf; then
echo -e "events {\n worker_connections ${nginx_limit_base};\n multi_accept on;\n use epoll;\n}" >> $nginx_conf
else
if ! grep -q "worker_connections" $nginx_conf; then
sed -i "/events {/a\ worker_connections ${nginx_limit_base};" $nginx_conf
else
sed -i "/worker_connections/c\ worker_connections ${nginx_limit_base};" $nginx_conf
fi
if ! grep -q "multi_accept" $nginx_conf; then
sed -i "/events {/a\ multi_accept on;" $nginx_conf
fi
if ! grep -q "use epoll;" $nginx_conf; then
sed -i "/events {/a\ use epoll;" $nginx_conf
fi
fi
}
# 更新 systemd 服务文件
function update_nginx_override_conf() {
# 计算 LimitNOFILE 值
cpu_cores=$(nproc)
limit_nofile=$((cpu_cores * 51200))
systemd_dir="/etc/systemd/system/nginx.service.d"
if [ ! -d "$systemd_dir" ]; then
mkdir -p "$systemd_dir"
fi
# 创建或修改 override.conf 文件,动态设置 LimitNOFILE
echo -e "[Service]\nLimitNOFILE=$limit_nofile" > "${systemd_dir}/override.conf"
# 重新加载 systemd 配置并尝试重启 Nginx
systemctl daemon-reload
systemctl restart nginx
echo "Nginx and systemd configuration updated successfully."
}
update_nginx_conf && update_nginx_override_conf && wait && rm -rf $CUSTOM_FILNAME