Skill: xieyuanqing/nginx-config-creator (version 0.1.1)

--- name: nginx-config-creator description: "Creates a standard Nginx/OpenResty reverse proxy config file for a service and reloads the web server. Features safety checks and environment awareness. Takes service name, domain, and port as main arguments." metadata: openclaw: requires: bins: ["bash", "docker"] --- # Nginx Config Creator (Enterprise Grade) This skill automates the creation of Nginx/OpenResty reverse proxy configurations. It is designed for both ease of use and safety, incorporating environment awareness and a critical safety-check mechanism. ## Features - **Environment Awareness**: Simplifies commands by reading configuration from environment variables. - **Safety Check**: Includes a '熔断' (fuse) mechanism. It tests the configuration before applying it and automatically rolls back if the test fails, preventing web server downtime. ## Pre-requisites (Recommended) For maximum convenience, it is recommended to set the following environment variables on the host system: - `NGINX_CONFIG_PATH`: The absolute path to the Nginx `conf.d` directory. - `NGINX_CONTAINER_NAME`: The name of the running Nginx/OpenResty Docker container. If these are not set, they **must** be provided as command-line arguments. ## Core Action: `scripts/create-and-reload.sh` This script performs the entire operation. ### **Inputs (Command-Line Arguments)** - `--service-name`: (Required) The short name for the service (e.g., `grafana`). - `--domain`: (Required) The root domain name (e.g., `example.com`). - `--port`: (Required) The local port the service is running on (e.g., `3000`). - `--config-path`: (Optional) The path to Nginx's `conf.d` directory. **Overrides** the `NGINX_CONFIG_PATH` environment variable. - `--container-name`: (Optional) The name of the Nginx Docker container. **Overrides** the `NGINX_CONTAINER_NAME` environment variable. ### **Output** - **On Success**: Prints a step-by-step log of its actions and a final success message. - **On Failure**: Prints a descriptive error message to stderr and exits. If the failure occurs during the Nginx configuration test, the full error from `nginx -t` is displayed. ### **Execution Workflow** 1. **Parse Arguments & Environment**: The script gathers all necessary paths and names from command-line arguments and environment variables. 2. **Generate Config**: It creates the `.conf` file in the target directory. 3. **Test Config (Safety Check)**: It executes `nginx -t` inside the specified container. 4. **Decide & Act**: - If the test passes, it proceeds to reload Nginx via `nginx -s reload`. - If the test fails, it **automatically deletes the generated file (rolls back)** and reports the error. 5. **Report Result**: Informs the user of the final outcome. ### **Example Usage** **Scenario 1: Environment variables are pre-set** ```bash # Set for future convenience export NGINX_CONFIG_PATH="/path/to/your/nginx/conf.d" export NGINX_CONTAINER_NAME="your_nginx_container" # Now, the command is very simple: bash skills/nginx-config-creator/scripts/create-and-reload.sh \ --service-name "grafana" \ --domain "example.com" \ --port "3000" ``` **Scenario 2: No environment variables (providing all info via arguments)** ```bash bash skills/nginx-config-creator/scripts/create-and-reload.sh \ --service-name "grafana" \ --domain "example.com" \ --port "3000" \ --config-path "/path/to/your/nginx/conf.d" \ --container-name "your_nginx_container" ``` ### **Failure Strategy** - **Missing Arguments**: The script will exit with an error if required arguments/environment variables are missing. - **`nginx -t` Fails**: The skill is designed to be safe. It will **not** attempt to reload a broken configuration. It will clean up after itself and show you the exact error, ensuring the live web server is never affected.

path: README.md
content: # Nginx Config Creator Skill

This is an enterprise-grade skill for AI agents that automates the creation of Nginx/OpenResty reverse proxy configurations. It is designed for both ease of use and operational safety.

## Features

- **Environment Awareness**: Simplifies commands by sourcing configuration from environment variables, falling back to command-line arguments.
- **Safety Fuse Mechanism**: The script performs a configuration test (`nginx -t`) before applying any changes. If the test fails, it automatically deletes the newly created config file (rolls back), ensuring the web server's stability is never compromised.
- **Stateless & Parametric**: Built for automation, receiving all necessary configuration via arguments or environment variables.

## Prerequisites (Recommended)

For convenience, you can set the following environment variables on the host system. They can also be provided as command-line arguments.

```bash
export NGINX_CONFIG_PATH="/path/to/your/nginx/conf.d"
export NGINX_CONTAINER_NAME="your_nginx_container_name"
```

## Usage

The core logic is handled by the `scripts/create-and-reload.sh` shell script.

### Arguments

- `--service-name` (Required): The short name for the service (e.g., `grafana`).
- `--domain` (Required): The root domain name (e.g., `example.com`).
- `--port` (Required): The local port the service is running on (e.g., `3000`).
- `--config-path` (Optional): The absolute path to Nginx's `conf.d` directory. Overrides the `NGINX_CONFIG_PATH` environment variable.
- `--container-name` (Optional): The name of the Nginx Docker container. Overrides the `NGINX_CONTAINER_NAME` environment variable.

### Example

Assuming environment variables are set, the command is very clean:

```bash
bash scripts/create-and-reload.sh \
  --service-name "prometheus" \
  --domain "example.com" \
  --port "9090"
```

## Included in this Skill

- `SKILL.md`: The manifest file for the AI agent.
- `scripts/create-and-reload.sh`: The robust shell script that handles config creation, testing, and reloading.


path: scripts/create-and-reload.sh
content: #!/bin/bash
#!/bin/bash

set -e # Exit immediately if a command exits with a non-zero status.

# --- Default values ---
SERVICE_NAME=""
DOMAIN_NAME=""
PORT=""
CONFIG_PATH=""
CONTAINER_NAME=""

# --- Parse Command-Line Arguments ---
while [[ "$#" -gt 0 ]]; do
    case $1 in
        --service-name) SERVICE_NAME="$2"; shift ;;
        --domain) DOMAIN_NAME="$2"; shift ;;
        --port) PORT="$2"; shift ;;
        --config-path) CONFIG_PATH="$2"; shift ;;
        --container-name) CONTAINER_NAME="$2"; shift ;;
        *) echo "Unknown parameter passed: $1"; exit 1 ;;
    esac
    shift
done

# --- Validate Core Arguments ---
if [ -z "$SERVICE_NAME" ] || [ -z "$DOMAIN_NAME" ] || [ -z "$PORT" ]; then
    echo "Error: --service-name, --domain, and --port are required." >&2
    exit 1
fi

# --- Environment Awareness: Use environment variables as fallback ---
CONFIG_PATH=${CONFIG_PATH:-$NGINX_CONFIG_PATH}
CONTAINER_NAME=${CONTAINER_NAME:-$NGINX_CONTAINER_NAME}

if [ -z "$CONFIG_PATH" ]; then
    echo "Error: Nginx config path is not set. Use --config-path or set NGINX_CONFIG_PATH environment variable." >&2
    exit 1
fi

if [ -z "$CONTAINER_NAME" ]; then
    echo "Error: Nginx container name is not set. Use --container-name or set NGINX_CONTAINER_NAME environment variable." >&2
    exit 1
fi

# --- Main Logic ---
FULL_DOMAIN="${SERVICE_NAME}.${DOMAIN_NAME}"
CONF_FILE_PATH="${CONFIG_PATH}/${SERVICE_NAME}.conf"

echo "--- Operation Summary ---"
echo "Service Name: $SERVICE_NAME"
echo "Full Domain: $FULL_DOMAIN"
echo "Target Port: $PORT"
echo "Config File: $CONF_FILE_PATH"
echo "Nginx Container: $CONTAINER_NAME"
echo "-------------------------"

# Create Nginx config content
NGINX_CONF=$(cat <<EOF
server {
    listen 80;
    server_name ${FULL_DOMAIN};

    location / {
        proxy_pass http://127.0.0.1:${PORT};
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
EOF
)

echo "Writing config to ${CONF_FILE_PATH}..."
echo "$NGINX_CONF" > "$CONF_FILE_PATH"
echo "[OK] Config file written."

# --- Safety Check (熔断机制) ---
echo "Testing Nginx configuration..."
if docker exec "$CONTAINER_NAME" nginx -t &> /tmp/nginx_test_output.log; then
    echo "[OK] Configuration test successful."
    
    # --- Apply Configuration ---
    echo "Reloading Nginx service..."
    if docker exec "$CONTAINER_NAME" nginx -s reload; then
        echo "[SUCCESS] Nginx reloaded successfully. Service '${SERVICE_NAME}' is now live at http://${FULL_DOMAIN}"
    else
        echo "Error: Failed to reload Nginx. Rolling back..." >&2
        rm -f "$CONF_FILE_PATH"
        echo "[ROLLED BACK] Deleted ${CONF_FILE_PATH}" >&2
        exit 1
    fi
else
    echo "Error: Nginx configuration test failed. Rolling back..." >&2
    cat /tmp/nginx_test_output.log >&2
    rm -f "$CONF_FILE_PATH"
    echo "[ROLLED BACK] Deleted ${CONF_FILE_PATH}" >&2
    exit 1
fi

rm -f /tmp/nginx_test_output.log

exit 0


Security scan verdicts — ClawScan: suspicious | static analysis: clean | VirusTotal: suspicious | SkillSpector: clean (LOW)