# Manage Custom Nginx Rules with the xCloud Public API and MCP

> List, test, create and update a site's custom Nginx rules from the xCloud Public API or an MCP agent. A rule that fails the Nginx test is never saved.

You can now read, test, create and update a site's **custom Nginx rules** from the xCloud Public API or from an AI agent connected to xCloud MCP. These are the same rules you add in the dashboard under **Tools → Nginx Customization**, with the same checks. xCloud writes the rule, runs the Nginx config test (`nginx -t`) and saves it only when the test passes. A rule that fails the test is never saved, and your live config stays as it was. This guide is for developers and agencies who manage Nginx rules across many sites from scripts, CI or an agent.

## What the API and MCP can do

| Task | Public API | MCP operation | Unified profile tool |
|---|---|---|---|
| List a site's rules | `GET /sites/{uuid}/custom-nginx` | `sites.customNginx` | `execute` |
| Test a rule without saving | `POST /sites/{uuid}/custom-nginx/test` | `sites.customNginx.test` | `execute` |
| Create a rule | `POST /sites/{uuid}/custom-nginx` | `sites.customNginx.store` | `execute` |
| Update a rule | `PUT /sites/{uuid}/custom-nginx` | `sites.customNginx.update` | `execute` |
| Turn a rule off or on | Dashboard only | Not available | Not available |
| Delete a rule | Dashboard only | Not available | Not available |

Create and update work the same way: a rule is identified by its `type` and `file`. Sending a rule with a `type` and `file` that already exist replaces that rule's content. A new pair creates a new rule.

Every write previews first. Without `confirm: true`, xCloud checks the rule and shows you what would run, and nothing changes. The response tells you the result directly; there is nothing to poll.

## Prerequisites
- A site on a server that uses Nginx. On sites that do not use Nginx, test, create and update are refused with `403`, and the list comes back empty.
- A team role that can manage custom Nginx on that site (the team owner, or a team member whose role includes the custom Nginx permission for that site).
- For the Public API: a token with `read:sites` and `write:sites`. Follow [How to access the xCloud API](/docs/how-to-access-the-xcloud-api/).
- For xCloud MCP: a client connected with write access. Follow [How to connect xCloud MCP to your AI agent](/docs/how-to-connect-xcloud-mcp-to-ai-agent/).
- Nginx config regeneration turned on for the site (see [Troubleshooting](#troubleshooting)).
- A tested Nginx directive and a copy of any rule you plan to replace.

## Rule fields

| Field | Values |
|---|---|
| `type` | Where xCloud places the rule: `server` (inside the server block), `main_location`, `php`, `proxy`, `before`, `after`, `7g` or `8g`. The dashboard shows only the types that fit the site, but the API accepts all 8 values, so pick the type that matches your site. See [choose the correct insertion point](/docs/configure-custom-nginx-xcloud/#choose-the-correct-insertion-point). |
| `file` | File name without an extension, up to 30 characters: letters, digits, `_` and `-`. |
| `template` | `Use My Own Config` for your own rule. A built-in template needs its dashboard content. |
| `content` | The Nginx directives. |
| `confirm` | Leave it out (or `false`) for a preview. Set `true` only after you review the preview. |

Members who can manage sites but not server settings get extra checks, the same as in the dashboard. They cannot use `before` or `after` or `include`, or directives outside the allowed list. `root` and `alias` must point inside the site's own folders, and `proxy_pass` must use a public IP address or the site's own PHP socket. See [rules checked for members with site-level access](/docs/configure-custom-nginx-xcloud/#rules-checked-for-members-with-site-level-access).

## Manage rules through the Public API

### Step 1: Set the API variables

Set the base URL, your token and the site UUID in your shell:

```bash
export XCLOUD_API_BASE='https://app.xcloud.host/api/v1'
export XCLOUD_API_TOKEN='xc_live_xxxxxxxxxxxx'
export SITE_UUID='11111111-1111-4111-8111-111111111111'
```

Find the site UUID with `GET /sites` or the [fleet overview of all servers and sites](/docs/get-fleet-overview-with-xcloud-public-api-and-mcp/). If your token covers several teams, add the `X-Team-Id` header to pick one (see [multi-team API tokens](/docs/multi-team-api-tokens-and-mcp-access/)).

**Expected result:** your shell holds the values the next requests use. The token is not printed.

### Step 2: List the existing rules

Check what the site already has before you add anything:

```bash
curl --fail-with-body -sS \
  -H "Authorization: Bearer ${XCLOUD_API_TOKEN}" \
  -H 'Accept: application/json' \
  "${XCLOUD_API_BASE}/sites/${SITE_UUID}/custom-nginx"
```

**Expected result:** `data.items` lists each rule with its `uuid`, `type`, `file`, `template`, `content`, `status` and `is_active`, and `data.count` shows how many there are. Note the `type` and `file` of any rule you plan to change.

### Step 3: Preview a rule

Save the rule in a file first, so quotes in the Nginx directives stay intact. This example stops browsers from caching a service worker file:

```bash
cat > rule.json <<'EOF'
{
  "type": "server",
  "file": "sw-no-cache",
  "template": "Use My Own Config",
  "content": "location = /sw.js { add_header Cache-Control \"no-cache\" always; }"
}
EOF

curl --fail-with-body -sS \
  -X POST \
  -H "Authorization: Bearer ${XCLOUD_API_TOKEN}" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  "${XCLOUD_API_BASE}/sites/${SITE_UUID}/custom-nginx/test" \
  --data @rule.json
```

**Expected result:** `200 OK` with `data.confirmation_required: true` and `data.preview` showing the site, file, type and first five lines of the rule. Nothing has run on the server. If the rule breaks a check (file name, type or a blocked directive), you get `422` with the reason instead, before anything runs.

### Step 4: Test the rule without saving it

Send the same request with `confirm: true`:

```bash
jq '. + {confirm: true}' rule.json > rule-confirmed.json

curl --fail-with-body -sS \
  -X POST \
  -H "Authorization: Bearer ${XCLOUD_API_TOKEN}" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  "${XCLOUD_API_BASE}/sites/${SITE_UUID}/custom-nginx/test" \
  --data @rule-confirmed.json
```

**Expected result:** `200 OK` with `data.test_successful: true`, `data.test_only: true` and the `nginx -t` result in `data.output`. xCloud puts the previous file back after the test, so nothing is saved and Nginx is not reloaded. This is the same as **Run & Debug** in the dashboard.

### Step 5: Save the rule

When the test passes, send the confirmed rule to the create endpoint:

```bash
curl --fail-with-body -sS \
  -X POST \
  -H "Authorization: Bearer ${XCLOUD_API_TOKEN}" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  "${XCLOUD_API_BASE}/sites/${SITE_UUID}/custom-nginx" \
  --data @rule-confirmed.json
```

**Expected result:** `200 OK` with `data.test_successful: true` and `data.test_only: false`. xCloud saved the rule, turned it on and reloaded Nginx. Check it with `curl -I https://example.com/sw.js`, which should show `Cache-Control: no-cache`.

### Step 6: Update a rule

Send the full new `content` with the same `type` and `file` to `PUT`. First edit `content` in `rule.json` and preview the change without `confirm`. This example also stops browsers from storing the file:

```bash
jq '.content = "location = /sw.js { add_header Cache-Control \"no-cache, no-store\" always; }"' \
  rule.json > rule-updated.json

curl --fail-with-body -sS \
  -X PUT \
  -H "Authorization: Bearer ${XCLOUD_API_TOKEN}" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  "${XCLOUD_API_BASE}/sites/${SITE_UUID}/custom-nginx" \
  --data @rule-updated.json
```

**Expected result:** `200 OK` with `data.confirmation_required: true` and a preview of the new content. Nothing has changed yet.

When the preview looks right, send the same request with `confirm: true`:

```bash
jq '. + {confirm: true}' rule-updated.json > rule-updated-confirmed.json

curl --fail-with-body -sS \
  -X PUT \
  -H "Authorization: Bearer ${XCLOUD_API_TOKEN}" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  "${XCLOUD_API_BASE}/sites/${SITE_UUID}/custom-nginx" \
  --data @rule-updated-confirmed.json
```

**Expected result:** `200 OK` with `data.test_successful: true`. The new content replaces the old one. If the Nginx test fails, the old rule stays live and unchanged.

## Manage rules through xCloud MCP

MCP uses the same checks as the Public API. Connect your client to `https://app.xcloud.host/mcp`, or `https://app.xcloud.host/mcp?profile=unified` when your client limits the number of tools. The unified profile lists three tools: `search`, `execute` and `docs`. The old `?profile=compact` address now opens the same unified profile; the old compact tool names still work there but are no longer listed.

### Step 1: Ask the agent to read the current rules

Start with a read so the agent knows what exists:

```text
List the custom Nginx rules on my site example.com in xCloud. Show each rule's type, file name and content. Do not change anything yet.
```

The agent finds the site and runs `sites.customNginx` (through the `execute` tool on the unified profile).

**Expected result:** the agent shows each rule, or says the site has none.

### Step 2: Ask for a test first

Describe the rule and ask the agent to test it before it saves anything:

```text
On example.com, add a custom Nginx rule in the server block, file name sw-no-cache, that sends Cache-Control: no-cache for /sw.js. Test it first and show me the preview before you run anything.
```

The agent calls `sites.customNginx.test` without `confirm`. xCloud answers with a preview and a `fingerprint`, and nothing runs.

**Expected result:** the agent shows you the site, file, type and first lines of the rule and asks for your approval.

### Step 3: Approve the test, then the save

After you approve, the agent sends the call again with `confirm: true` and the preview's `fingerprint`. If anything changed since the preview, nothing runs and a new preview comes back. When the test passes, ask the agent to save the rule. It previews `sites.customNginx.store` (or `sites.customNginx.update`) the same way and runs it only after your approval.

**Expected result:** the agent reports `test_successful: true`, or the error code and the `nginx -t` output so you can fix the rule.

The AI Assistant inside the xCloud dashboard can list custom Nginx rules but cannot test or save them. Use an agent connected to xCloud MCP, the Public API, or the [dashboard](/docs/configure-custom-nginx-xcloud/) for changes.

## Troubleshooting

| Response | Cause | Fix |
|---|---|---|
| `422` `nginx_config_test_failed` | Nginx rejected the rule. Nothing was saved and the previous file is back in place. | Read `errors.output` for the failing line and `errors.next` for the next step, fix the rule and test again. |
| `422` "Please enable Nginx regeneration from your site settings" | Nginx config regeneration is turned off for this site. | Open the site, go to **Nginx and Security** and turn off **Disable Nginx Config Regeneration**. See [config regeneration failures](/docs/how-to-troubleshoot-nginx-configuration-regeneration-failures-with-xcloud/). |
| `422` with a reason such as "Use a public IP address for proxy_pass…" or "This directive requires server settings permission…" | Your role can manage sites but not server settings, and the rule uses something that needs that permission. | Change the rule, or ask a team member who can manage server settings to add it. |
| `422` validation error on `type`, `file`, `template` or `content` | A field is missing, or the file name has a dot, space or other blocked character. | Read `errors` for the field name and fix that value. |
| `200` with `confirmation_required: true` | The request had no `confirm: true`, so xCloud only previewed it. | Review the preview, then send the same request with `confirm: true`. |
| `403` "This is only available for Nginx servers." | The site is on a server that does not use Nginx. | Custom Nginx rules work only on Nginx servers. |
| `403` without that message | The token lacks `write:sites`, or your team role cannot manage custom Nginx. | Add the scope to the token or ask a team admin for the permission. |
| `404` | The site UUID does not exist in the selected team. | List your sites and use the UUID from the response, or set `X-Team-Id`. |

## Frequently asked questions

### What happens if my custom Nginx rule fails the Nginx test?

Nothing is saved. xCloud writes the rule, runs nginx -t and, when the test fails, puts the previous file back. Your site keeps serving with its old config, and the API returns 422 with errors.code set to nginx_config_test_failed and the test output in errors.output.

### Why did my request return a preview instead of saving the rule?

Every custom Nginx write previews first. A request without confirm set to true checks the rule and returns data.preview with the site, file, type and first five lines, and changes nothing. Send the same request again with confirm set to true to run it.

### Can I turn off or delete a custom Nginx rule through the API?

Not yet. The API and MCP can list, test, create and update rules. Turning a rule off or on and deleting it are only available in the dashboard under Tools → Nginx Customization.

### Can the AI Assistant in the xCloud dashboard change my Nginx rules?

No. The dashboard AI Assistant can list a site's custom Nginx rules, but it cannot test or save them. Use an AI agent connected to xCloud MCP with write access, the Public API, or the dashboard.

## Next steps
- Add, test, turn off and delete rules in the dashboard: [How to configure custom Nginx in xCloud](/docs/configure-custom-nginx-xcloud/).
- Push one rule to many sites at once with [bulk Nginx customization](/docs/how-to-apply-bulk-nginx-customization/).
- Add headers and redirects without writing Nginx: [Site Rules via the API and MCP](/docs/manage-site-rules-with-xcloud-public-api-and-mcp/).
- Review the full [xCloud Public API reference](https://app.xcloud.host/api/v1/docs).
- Release notes: [v3.0.2](/changelog/v3-0-2/) added custom Nginx rules to the API and MCP.

If you run into any issues with custom Nginx rules through the API or MCP, feel free to reach out to our [support team](/docs/access-built-in-support-portal-in-xcloud/) with the site UUID and the error code you received.
