#!/bin/bash # 幂等地把 deploy/nginx/tb-location.conf 插入 /etc/nginx/sites-available/juzi。 # 只做「插入一段 location」,不改动任何已有 location;先备份,nginx -t 通过才 reload。 # # 用法(在 1me.xajuzi.com 上): # scp deploy/add-tb-nginx.sh deploy/nginx/tb-location.conf ubuntu@1me.xajuzi.com:/tmp/ # ssh ubuntu@1me.xajuzi.com "sed -i 's/\r$//' /tmp/add-tb-nginx.sh && bash /tmp/add-tb-nginx.sh" set -euo pipefail CONF=/etc/nginx/sites-available/juzi SNIPPET=${1:-/tmp/tb-location.conf} STAMP=$(date +%Y%m%d%H%M%S) BACKUP="$CONF.bak-tb-$STAMP" if grep -q 'location /tb/' "$CONF"; then echo "SKIP: /tb/ 已存在,不重复插入" exit 0 fi if [ ! -f "$SNIPPET" ]; then echo "缺少片段文件: $SNIPPET" >&2 exit 1 fi sudo cp "$CONF" "$BACKUP" echo "BACKUP: $BACKUP" sudo python3 - "$CONF" "$SNIPPET" <<'PY' import sys conf_path, snippet_path = sys.argv[1], sys.argv[2] text = open(conf_path, encoding='utf-8').read() snippet = open(snippet_path, encoding='utf-8').read() block = "# BEGIN taobao-debug 2026-11-01\n" + snippet + "# END taobao-debug 2026-11-01\n\n" marker = " location / {\n" if marker not in text: sys.exit("marker 'location / {' not found") text = text.replace(marker, block + marker, 1) open(conf_path, 'w', encoding='utf-8').write(text) print("INSERTED") PY echo "--- nginx -t ---" sudo nginx -t echo "--- reload ---" sudo systemctl reload nginx echo "RELOADED"