mac 防止brew 安装 nginx 后不通过服务直接启动

  • 直接看代码~,逻辑很简单,把原命令替换掉
NGINX_BIN=$(which nginx)
sudo mv "$NGINX_BIN" "${NGINX_BIN}.bin"
sudo tee "$NGINX_BIN" << 'EOF'
#!/bin/bash

NGINX_REAL="${BASH_SOURCE[0]}.bin"

if [[ ! -x "$NGINX_REAL" ]]; then
  echo "❌ 找不到原始 nginx: $NGINX_REAL" >&2
  exit 1
fi

# === 白名单:只允许明确的安全命令 ===
case "$1" in
  -t|--test|\
  -T|--test-full|\
  -v|--version|\
  -V|--verbose|\
  -h|--help)
    exec "$NGINX_REAL" "$@"
    ;;
esac

# === 特殊处理:nginx -s reload/stop 等信号操作 ===
if [[ "$1" == "-s" ]]; then
  case "${2:-}" in
    stop|quit|reopen|reload)
      exec "$NGINX_REAL" "$@"
      ;;
  esac
fi

# === 所有其他情况(包括无参数!)都拦截 ===
echo "⚠️  检测到可能启动 Nginx 实例的操作!"
echo "💡 请使用 Homebrew Services 管理:"
echo "     brew services start nginx"
echo "     brew services stop nginx"
echo ""
echo "🔧 调试用法(绕过拦截):"
echo "     $NGINX_REAL $*"
echo ""

# 如果是无参数,特别说明
if [[ $# -eq 0 ]]; then
  echo "❓ 你运行了 'nginx'(无参数),这会启动新实例!"
fi

read -p "是否继续? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
  exec "$NGINX_REAL" "$@"
else
  echo "🚫 已取消。"
  exit 1
fi
EOF
sudo chmod +x "$NGINX_BIN"


posted @ 2025-11-30 16:04  铁流是宝宝  阅读(2)  评论(0)    收藏  举报