# Host your frontend and API on the same domain without Nginx

Gregale · Updated 2026-12-03

Use one hostname and route /api to a separate Gregale app. Deploy a new API version without redeploying the frontend.

**You can serve a frontend at `/` and route `/api/**` to a separately deployed backend on the same hostname.** A Gregale edge route sends the request to the API app without redirecting the browser. The frontend uses relative URLs, so this browser-to-API call is same-origin and doesn't depend on a CORS allowlist.

Use this when a frontend and API have separate deployment cycles but belong to one site. It isn't how you make a backend private: the direct API hostname and the routed path are separate access points.

## Put the API behind the frontend hostname

Deploy the two apps in the same Gregale workspace and ownership scope. Add a `route` rule to the frontend host that selects only the API prefix:

```json
{
  "kind": "route",
  "match_host": "frontend.example.com",
  "match_path": "/api/**",
  "action": { "target_app_slug": "my-api" }
}
```

With this rule, a request for `https://frontend.example.com/api/status` is served by `my-api` with the path `/api/status` intact. The browser never has to call the API's own hostname. If the backend already serves `/status` instead, use a deliberate [rewrite rule](https://gregale.dev/docs/edge-rules.md#rewrite) and verify the received path. A route rule by itself doesn't strip the prefix.

Use an account API key with access to both apps to write the cross-app rule. An app-scoped deploy token cannot route to another app. If you already have broader routing rules, review precedence before adding one and check paths just outside the prefix.

## Try a separate API release

[Download the two-app Python starter](https://gregale.dev/examples/same-origin-frontend-api.zip). Run it in a disposable workspace with the [Gregale CLI](https://gregale.dev/docs/install.md), Bash, curl, jq, OpenSSL and Python 3. Set `GREGALE_TOKEN` to an account key authorized to create, deploy, route and delete test apps. The fixture contains no secrets or real data and deliberately creates two public app URLs using generated Gregale hostnames. It doesn't touch DNS or existing apps.

```bash
bash run-demo.sh
```

The frontend calls `fetch('/api/status')`. The script checks that the frontend returned 404 on that path before routing, adds the edge rule, then verifies that the same URL returns the API's v1 JSON. It deploys only the API with a v2 marker and verifies the new JSON through the frontend hostname. The frontend HTML and its serving deployment ID must stay unchanged. A separate check confirms that `/apiary` still returns 404 at the shared hostname. Cleanup deletes only the two apps the script can identify from their create receipts.

## Keep auth on the path that actually serves the request

For routed requests, Gregale applies the **target app's** `@app` rules, not the source app's `@app` rules. Host-specific rules still apply to that host. Put a protection on the intended host path or on the target app as appropriate, then verify both the frontend URL and the API's direct URL using the access policy you intend. Same-origin routing does not verify a user's identity, prevent cross-site request forgery, or make the API's direct hostname private.

This example uses two releases, not one coordinated rollout. Keep API changes compatible with the frontend that is already live. If the two versions must change together, see [atomic project releases](https://gregale.dev/docs/project-releases.md).

See [edge routing](https://gregale.dev/docs/edge-rules.md#route), [CORS for APIs that intentionally use separate origins](https://gregale.dev/articles/fix-cors-errors-without-changing-backend-code), and [how to verify the exact serving deployment](https://gregale.dev/articles/verify-production-deployment).

## Questions

### Can a frontend and backend use the same domain?

Yes. Serve the frontend at / and configure a path rule so requests under /api are served by a separately deployed backend.

### Does routing /api remove the prefix?

No. A Gregale route rule selects the app and preserves the request path. Use a separate rewrite if the backend expects a different path.

### Does a same-domain route make the API private?

No. Use access controls on the target app or intended hostname and verify the backend’s direct URL separately.

Canonical URL: https://gregale.dev/articles/host-frontend-and-api-on-same-domain

