# Serve Source Configuration on Custom Domains

This guide covers the steps to use your own domain instead of the RudderStack domains for fetching the source configuration response.

## Setup overview

When the RudderStack SDK is loaded, it uses the source write key to fetch the required source configuration from RudderStack. For this reason, the distribution settings in this scenario are slightly different as you need to explicitly allowlist the **Authorization** header to make sure it is sent along with each request.

{{< info >}}
The SDK makes a `GET` request to the `https://api.rudderstack.com/sourceConfig` URL to fetch the source configuration and uses the write key as the authorization header.
{{< /info >}}

Create a new distribution by following these steps:

1. Log in to your [AWS console](https://aws.amazon.com/console/).
2. Click **Services** and go to **Network & Content Delivery** > **CloudFront**.
3. Click **Create a CloudFront distribution**.

The following table gives a high-level overview of the required cache policy, origin request policy, and response headers policy for fetching the source configuration response:

| Cache policy | Origin request policy | Response headers policy (optional) |
| :------| :--------| :----------- | 
| [Cache policy settings](#cache-policy-settings)  | [Origin request policy settings](#source-origin-request-policy-settings) | `CORS-With-Preflight` |

## Step 1: Configure distribution

{{< image src="images/user-guides/custom-domains/source-config-distribution-settings.webp" alt="Custom domains distribution settings" >}}

The following sections highlight the required distribution settings.

#### Origin

| Field                  | Setting              |
| :--------------------- | :------------------- |
| Origin domain     | `api.rudderstack.com` |
| Protocol | `HTTPS Only`           |
| HTTPS port | `443` |
| Minimum origin SSL protocol | `TLSv1.2` |
| Name | `api.rudderstack.com` |
| Enable Origin Shield | `No` |

#### Default cache behavior settings

{{< image src="images/user-guides/custom-domains/custom-domains-2-new.webp" alt="Custom domains distribution settings" >}}

| Field     | Setting                 |
| :---------- | :---------------------- |
| Compress objects automatically | `Yes` |
| Viewer protocol policy | `Redirect HTTP to HTTPS` |
| Allowed HTTP methods | `GET`, `HEAD`, `OPTIONS`, `PUT`, `POST`, `PATCH`, `DELETE` |
| Restrict viewer access | `No` |

#### Cache key and origin requests

Select **Cache policy and origin request policy (recommended)** and configure the following settings:

| Field | Setting |
| :------| :------|
| Cache policy |  See [Cache policy settings](#cache-policy-settings) |
| Origin request policy | See [Origin request policy settings](#source-origin-request-policy-settings) | 
| Response headers policy | `CORS-With-Preflight` | 

#### Cache policy settings

Create a cache policy with the following settings:

{{< image src="images/user-guides/custom-domains/custom-domains-4-latest.webp" alt="Custom domains cache policy settings" >}}

| Field       | Setting |
| :------- | :------------------- |
| Name | `<CACHE_POLICY_NAME>` | 
| Description | `<CACHE_POLICY_DESCRIPTION>` |
| Minimum TTL | `1` |
| Maximum TTL | `86400` |
| Default TTL | `300` |
| Headers | `Include the following headers` | 
| Add header | `Authorization` <br />`Origin` |
| Query strings | `All` |
| Cookies | `None` |

#### Origin request policy settings {#source-origin-request-policy-settings}

Create a new origin request policy with the following settings:

{{< image src="images/user-guides/custom-domains/custom-domains-origin-request-policy.webp" alt="Origin request policy settings" >}}

| Field       | Setting |
| :------- | :------------------- |
| Name | `rudderstack-allow-headers` | 
| Headers | `Include the following headers` | 
| Add header | <ul><li>`Access-Control-Request-Headers`</li><li>`Access-Control-Request-Method`</li><li>`Origin`</li><li>`Content-Encoding`</li></ul> |
| Query strings | `All` |
| Cookies | `None` |

{{< warning >}}
For non-AWS setups, you may also need to add the `Authorization` header to the origin request policy.
{{< /warning >}}

#### Additional distribution settings

{{< customreadfile "/includes/custom-domains/additional-distribution-settings.md" >}}

## Step 2: Deploy distribution

{{< customreadfile "/includes/custom-domains/deploy-distribution.md" >}}

## Step 3: Fetch source configuration

To use a custom URL to fetch the source configuration, add it as an option when loading the SDK.

An example of how to use a custom URL to fetch the JavaScript SDK source configuration is shown below:

```javascript
rudderanalytics.load(
  SOURCE_WRITE_KEY,
  DATA_PLANE_URL, {
    configUrl: "https://<YOUR_CUSTOM_DOMAIN>",
  }
)
```

## Use custom SSL certificates

{{< customreadfile "/includes/custom-domains/custom-ssl-certificates.md" >}}

## Custom request header for GCP external load balancer

{{< customreadfile "/includes/custom-domains/gcp-load-balancer.md" >}}

