/** * On H5 nodes: avoid unconditional HTTP→HTTPS redirect when behind CLB * (CLB terminates TLS and forwards HTTP). Prefer X-Forwarded-Proto. */ import { loadTencentCredentials, runShellCommand } from './tencent-lib.mjs'; const SCRIPT = `#!/bin/bash set -euo pipefail echo "HOST=$(hostname)" CONF="" for f in /etc/nginx/sites-enabled/* /etc/nginx/conf.d/*.conf; do [ -f "$f" ] || continue if grep -qE 'server_name|listen 80' "$f" 2>/dev/null; then CONF="$f"; break; fi done echo "CONF=$CONF" if [ -z "$CONF" ]; then echo 'no nginx site conf'; exit 0; fi cp -a "$CONF" "$CONF.bak.$(date +%s)" # Replace bare 'return 301 https://...' with proto-aware redirect python3 - <<'PY' import pathlib, re, sys paths=[] for p in pathlib.Path('/etc/nginx/sites-enabled').glob('*'): paths.append(p) for p in pathlib.Path('/etc/nginx/conf.d').glob('*.conf'): paths.append(p) changed=False for path in paths: text=path.read_text(encoding='utf-8', errors='ignore') orig=text # common patterns text=re.sub( r'(?m)^(\\s*)return\\s+301\\s+https://\\$host\\$request_uri;\\s*$', r'\\1if ($http_x_forwarded_proto != "https") { return 301 https://$host$request_uri; }', text, ) text=re.sub( r'(?m)^(\\s*)return\\s+301\\s+https://www\\.xdjchuanhui\\.com\\$request_uri;\\s*$', r'\\1if ($http_x_forwarded_proto != "https") { return 301 https://www.xdjchuanhui.com$request_uri; }', text, ) if text!=orig: path.write_text(text, encoding='utf-8') print('patched', path) changed=True if not changed: print('no unconditional 301 https redirect found (or already patched)') PY nginx -t systemctl reload nginx curl -sS -o /dev/null -w 'local80=%{http_code}\\n' http://127.0.0.1/ || true curl -sS -o /dev/null -w 'as_https=%{http_code}\\n' -H 'X-Forwarded-Proto: https' -H 'Host: www.xdjchuanhui.com' http://127.0.0.1/ || true `; async function main() { const cred = loadTencentCredentials(); for (const id of ['ins-9zdly5wm', 'ins-hvq9s3gi']) { console.log('===', id); try { const out = await runShellCommand( { secretId: cred.secretId, secretKey: cred.secretKey }, 'ap-nanjing', [id], SCRIPT, 120 ); console.log(out); } catch (e) { console.error(id, e.message); } } } main().catch((e) => { console.error(e); process.exit(1); });