Skip to content

Commit 4c9f6fb

Browse files
authored
fix: sync nginx with daemon file on hot-reload flows
- add idempotent nginx_sync to start, reload, or stop nginx based on the final config state - start nginx automatically when adding WS + Argo protocols, removing the need for a manual systemctl restart - regenerate the daemon file after the final nginx state is decided in change_protocols, keeping systemd ExecStartPre / OpenRC start_pre consistent and clearing stale OpenRC entries on Alpine after WS removal - converge subscription toggle ON/OFF to daemon regeneration plus nginx_sync, replacing inline sed patches - unify nginx management on CentOS7 with a tolerant ExecStartPre ("-" prefix) so nginx starts on reload and after reboot - reload nginx after nginx.conf rewrites (port changes, Argo type changes, additional WS protocols) - stop nginx during uninstall to cover processes started outside the service cgroup fix: 热更流程下 nginx 与守护文件状态同步 - 新增幂等 nginx_sync,按最终配置状态自动启动 / 热重载 / 停止 nginx - 添加 WS + Argo 协议时自动拉起 nginx,无需再手动 systemctl restart - change_protocols 先确定最终 nginx 状态再生成守护文件,保证 systemd ExecStartPre / OpenRC start_pre 一致,并清除 Alpine 删除 WS 后残留的 start_pre - 订阅开关 ON/OFF 收敛为守护文件重生成 + nginx_sync,移除内联 sed 补丁 - CentOS7 统一走带 "-" 容错的 ExecStartPre,reload 与重启后 nginx 均能拉起 - nginx.conf 被重写(改端口 / 换 Argo 隧道 / 追加 WS 协议)后自动 reload nginx - 卸载时显式停止 nginx,覆盖服务 cgroup 外启动的进程
1 parent 8d893d6 commit 4c9f6fb

1 file changed

Lines changed: 43 additions & 41 deletions

File tree

sing-box.sh

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
# 当前脚本版本号
4-
VERSION='v1.3.19 (2026.08.02)'
4+
VERSION='v1.3.19 (2026.08.04)'
55

66
# Github 反代加速代理
77
GITHUB_PROXY=('https://hub.glowp.xyz/' 'https://proxy.vvvv.ee/')
@@ -764,6 +764,7 @@ change_config() {
764764
done
765765
fetch_nodes_value
766766
[ -n "$PORT_NGINX" ] && export_nginx_conf_file
767+
nginx_sync
767768
cmd_systemctl reload sing-box
768769
[ -n "$ARGO_DOMAIN" ] && export_argo_json_file
769770
sync_firewall_rules
@@ -936,14 +937,14 @@ change_config() {
936937
# 判断是否还需要 nginx:有 WS 协议且 Argo 反代
937938
if { [ -n "$PORT_VMESS_WS" ] || [ -n "$PORT_VLESS_WS" ]; } && [ "$IS_ARGO" = 'is_argo' ]; then
938939
export_nginx_conf_file
939-
nginx -s reload -c ${WORK_DIR}/nginx.conf 2>/dev/null || nginx_run
940940
else
941941
nginx_stop
942-
[ "$SYSTEM" != 'Alpine' ] && [ "$IS_CENTOS" != 'CentOS7' ] && \
943-
sed -i '/ExecStartPre=.*nginx/d' ${SINGBOX_DAEMON_FILE} && systemctl daemon-reload
944942
rm -f ${WORK_DIR}/nginx.conf
945943
unset PORT_NGINX
946944
fi
945+
# 重新生成守护文件并同步 nginx(systemd ExecStartPre / OpenRC start_pre 与最终状态一致)
946+
sing-box_systemd
947+
nginx_sync
947948
/bin/rm -f ${WORK_DIR}/subscribe/qr
948949
export_list
949950
info " $(text 112) "
@@ -964,18 +965,9 @@ change_config() {
964965
fetch_nodes_value
965966
[ -z "$PORT_NGINX" ] && input_nginx_port
966967
export_nginx_conf_file
967-
# 更新服务文件 ExecStartPre(非 CentOS7)
968-
if [ "$SYSTEM" != 'Alpine' ] && [ "$IS_CENTOS" != 'CentOS7' ] && \
969-
! grep -q 'ExecStartPre=.*nginx' ${SINGBOX_DAEMON_FILE} 2>/dev/null; then
970-
sed -i '/^ExecStart=/i ExecStartPre='$(command -v nginx)' -c '${WORK_DIR}'/nginx.conf' ${SINGBOX_DAEMON_FILE}
971-
systemctl daemon-reload
972-
fi
973-
# 启动或 reload nginx
974-
if ps -eo pid,args | grep -qE "[n]ginx.*${WORK_DIR}/nginx.conf" 2>/dev/null; then
975-
nginx -s reload -c ${WORK_DIR}/nginx.conf 2>/dev/null || true
976-
else
977-
nginx_run
978-
fi
968+
# 重新生成守护文件并同步 nginx(含 Alpine / CentOS7,重启后 nginx 随服务拉起)
969+
sing-box_systemd
970+
nginx_sync
979971
export_list
980972
info " $(text 112) "
981973
fi
@@ -1909,6 +1901,7 @@ change_argo() {
19091901
# 更新节点信息和配置
19101902
fetch_nodes_value
19111903
export_nginx_conf_file
1904+
nginx_sync
19121905
export_list
19131906
}
19141907

@@ -2132,6 +2125,19 @@ nginx_stop() {
21322125
ss -nltp | sed -n "/pid=$NGINX_PID,/ s/,/ /gp" | grep -oP 'pid=\K\S+' | sort -u | xargs kill -9 >/dev/null 2>&1
21332126
}
21342127

2128+
# 让 nginx 进程与最终配置状态保持一致:需要则启动/热重载,不需要则停止
2129+
nginx_sync() {
2130+
if [ -s ${WORK_DIR}/nginx.conf ]; then
2131+
if ps -eo pid,args | grep -qE "[n]ginx.*${WORK_DIR}/nginx.conf" 2>/dev/null; then
2132+
nginx -s reload -c ${WORK_DIR}/nginx.conf 2>/dev/null || true
2133+
else
2134+
nginx_run
2135+
fi
2136+
else
2137+
nginx_stop
2138+
fi
2139+
}
2140+
21352141
# 为了适配 alpine,定义 cmd_systemctl 的函数
21362142
cmd_systemctl() {
21372143

@@ -2150,6 +2156,7 @@ cmd_systemctl() {
21502156
;;
21512157
reload )
21522158
rc-service "$2" reload >/dev/null 2>&1 || rc-service "$2" restart >/dev/null 2>&1
2159+
[ -s ${WORK_DIR}/nginx.conf ] && nginx_sync
21532160
local MAINPID=$(cat /var/run/sing-box.pid 2>/dev/null)
21542161
[ -n "$MAINPID" ] && info "\n $(text 95) \n"
21552162
;;
@@ -2162,21 +2169,13 @@ cmd_systemctl() {
21622169
case "$1" in
21632170
enable | disable )
21642171
systemctl "$1" --now "$2" >/dev/null 2>&1
2165-
if [ "$IS_CENTOS" = 'CentOS7' ] && [ "$2" = 'sing-box' ] && [ -s $WORK_DIR/nginx.conf ]; then
2166-
if [ "$1" = 'enable' ]; then
2167-
nginx_run
2168-
else
2169-
nginx_stop
2170-
fi
2171-
fi
21722172
;;
21732173
restart )
2174-
[ "$IS_CENTOS" = 'CentOS7' ] && [ "$2" = 'sing-box' ] && [ -s $WORK_DIR/nginx.conf ] && nginx_stop
21752174
systemctl restart "$2" >/dev/null 2>&1
2176-
[ "$IS_CENTOS" = 'CentOS7' ] && [ "$2" = 'sing-box' ] && [ -s $WORK_DIR/nginx.conf ] && nginx_run
21772175
;;
21782176
reload )
21792177
systemctl reload sing-box >/dev/null 2>&1 || systemctl restart sing-box >/dev/null 2>&1
2178+
[ -s ${WORK_DIR}/nginx.conf ] && nginx_sync
21802179
local MAINPID=$(systemctl show -p MainPID sing-box 2>/dev/null | awk -F= '{print $2}')
21812180
[ -n "$MAINPID" ] && [ "$MAINPID" -gt 0 ] 2>/dev/null && info "\n $(text 95) \n"
21822181
;;
@@ -4307,9 +4306,9 @@ start_pre() {
43074306
mkdir -p /var/run
43084307
chmod 755 /var/run"
43094308

4310-
# 如果配置了 Nginx,启动 Nginx
4309+
# 如果配置了 Nginx,启动 Nginx(nginx 已在运行时不阻塞服务启动)
43114310
[ -n "$PORT_NGINX" ] && OPENRC_SERVICE+="
4312-
$(command -v nginx) -c ${WORK_DIR}/nginx.conf"
4311+
$(command -v nginx) -c ${WORK_DIR}/nginx.conf || true"
43134312

43144313
OPENRC_SERVICE+="
43154314
# 确保 PID 文件不存在,避免启动失败
@@ -4358,7 +4357,8 @@ NoNewPrivileges=yes
43584357
TimeoutStartSec=0
43594358
WorkingDirectory=${WORK_DIR}
43604359
"
4361-
[[ -n "$PORT_NGINX" && "$IS_CENTOS" != 'CentOS7' ]] && SING_BOX_SERVICE+="ExecStartPre=$(command -v nginx) -c ${WORK_DIR}/nginx.conf
4360+
# 统一在 systemd 里用 ExecStartPre 管理 nginx(含 CentOS7);"-" 前缀容忍 nginx 已在运行等情况,避免阻塞服务启动
4361+
[[ -n "$PORT_NGINX" ]] && SING_BOX_SERVICE+="ExecStartPre=-$(command -v nginx) -c ${WORK_DIR}/nginx.conf
43624362
"
43634363
SING_BOX_SERVICE+="ExecStart=${WORK_DIR}/sing-box run -C ${WORK_DIR}/conf
43644364
ExecReload=/bin/kill -HUP \$MAINPID
@@ -5705,15 +5705,22 @@ change_protocols() {
57055705
unset PORT_NAIVE
57065706
fi
57075707

5708-
# 生成 Nginx 配置文件
5708+
# 生成各协议的 json 文件
5709+
sing-box_json change
5710+
5711+
# 无 ws 协议且无订阅时清理 nginx:先于守护文件生成,确保 ExecStartPre / start_pre 与最终状态一致
5712+
if ! ls ${WORK_DIR}/conf/*-ws*inbounds.json >/dev/null 2>&1 && [[ -s ${WORK_DIR}/nginx.conf && "$IS_SUB" = 'no_sub' ]]; then
5713+
nginx_stop
5714+
rm -f ${WORK_DIR}/nginx.conf
5715+
unset PORT_NGINX
5716+
fi
5717+
5718+
# 生成 Nginx 配置文件(按最终状态)
57095719
[ -n "$PORT_NGINX" ] && export_nginx_conf_file
57105720

57115721
# 重新生成 Sing-box 守护进程文件
57125722
sing-box_systemd
57135723

5714-
# 生成各协议的 json 文件
5715-
sing-box_json change
5716-
57175724
# 如有需要,安装和删除 Argo 服务
57185725
if ls ${WORK_DIR}/conf/*-ws*inbounds.json >/dev/null 2>&1; then
57195726
if [[ "$ARGO_OR_ORIGIN_RULES" != '2' && "$ARGO_ORIGIN_RULES_STATUS" != 'is_origin' && ! -s ${ARGO_DAEMON_FILE} ]]; then
@@ -5729,18 +5736,12 @@ change_protocols() {
57295736
fi
57305737
fi
57315738

5732-
# 无 ws 协议且无订阅时删除 nginx 配置并停止进程;有订阅保留(订阅服务需要 nginx)
5733-
if ! ls ${WORK_DIR}/conf/*-ws*inbounds.json >/dev/null 2>&1 && [[ -s ${WORK_DIR}/nginx.conf && "$IS_SUB" = 'no_sub' ]]; then
5734-
nginx_stop
5735-
[ "$SYSTEM" != 'Alpine' ] && [ "$IS_CENTOS" != 'CentOS7' ] && \
5736-
sed -i '/ExecStartPre=.*nginx/d' ${SINGBOX_DAEMON_FILE} && systemctl daemon-reload
5737-
rm -f ${WORK_DIR}/nginx.conf
5738-
unset PORT_NGINX
5739-
fi
5740-
57415739
# 热更 sing-box(SIGHUP 重新加载配置,PID 不变)
57425740
cmd_systemctl reload sing-box
57435741

5742+
# 同步 nginx 进程:需要则启动/热重载,不需要则停止
5743+
nginx_sync
5744+
57445745
# 打开防火墙相关端口
57455746
sync_firewall_rules
57465747

@@ -5759,6 +5760,7 @@ uninstall() {
57595760
if [ -d ${WORK_DIR} ]; then
57605761
[ -s ${ARGO_DAEMON_FILE} ] && cmd_systemctl disable argo &>/dev/null
57615762
[ -s ${SINGBOX_DAEMON_FILE} ] && cmd_systemctl disable sing-box &>/dev/null
5763+
nginx_stop
57625764
sleep 1
57635765
[[ -s ${WORK_DIR}/nginx.conf && "$(ps -ef | grep -c '[n]ginx')" = 0 ]] && reading "\n $(text 83) " REMOVE_NGINX
57645766
[ "${REMOVE_NGINX,,}" = 'y' ] && ${PACKAGE_UNINSTALL[int]} nginx >/dev/null 2>&1

0 commit comments

Comments
 (0)