# Mirror back-to-origin

> When a file doesn't exist in the bucket, fetch it from another origin and save it, for data migration without interrupting your business.

After you configure mirror back-to-origin, when someone requests a file that doesn't exist in a bucket, S4 fetches it from the origin you specify according to your rules, returns it to the requester, and saves it in the bucket. The next request reads directly from the bucket. It suits migrating existing files from another cloud storage service or a self-hosted server: you don't need to move everything in advance, and your business isn't affected.

To get there, go to **Object Storage (对象存储) › [Bucket list (桶列表)](https://console.bitiful.com/buckets)** › click the bucket name › **Mirror back-to-origin (镜像回源)**.

## How it works

1. A requester asks for a file with a GET request.
2. If the file is in the bucket, it is returned directly with no origin fetch.
3. If the file isn't in the bucket, S4 requests it from the origin according to the rules.
4. If the origin returns 200, the content is returned to the requester and saved to the bucket.
5. All later requests read from the bucket and don't go to the origin. When the file is updated at the origin, the file in the bucket isn't updated.

## Add a rule

### Open the dialog

Click **Add mirror back-to-origin (添加镜像回源)**.

### Enter the origin fetch condition

In **File name prefix (文件名前缀)**, enter a directory that must end with `/`, for example `images/`. Enter `/` for the whole bucket.

### Enter the origin address

Choose the protocol and enter the **Hostname (主机名)** and an optional **Directory (目录)**. The hint box below shows an example address for the current rule.

### Submit

Click **Submit (提交)**, and it takes effect immediately.

- `File name prefix` (text, required): Must end with `/`. Consecutive `/`, levels named `..`, and emoji aren't allowed, and the length can't exceed 128 characters. Each prefix can have only one rule. After saving, the list shows it beginning with `/`, for example `/images/`.

- `Protocol` (single choice, required, default: http): `http` or `https`.

- `Hostname` (text, required): The origin domain, for example `old.example.com`, without `http://`.

- `Directory` (text): A directory on the origin. It can be empty, and leading and trailing `/` are removed automatically. The full address assembled from the protocol, hostname, and directory is 10 \~ 256 characters.

You can't add rules while the account has an overdue balance. Rules can't be edited. To change one, **Delete (删除)** it and add it again.

## How addresses map

Matching **removes the file name prefix** and appends the remaining path to the origin address:

| File name prefix | Origin address                   | Requested in the bucket | Actual request to the origin             |
| :--------------- | -------------------------------- | ----------------------- | ---------------------------------------- |
| `/`              | `https://old.example.com`        | `a/b.png`               | `https://old.example.com/a/b.png`        |
| `/`              | `https://old.example.com/static` | `a/b.png`               | `https://old.example.com/static/a/b.png` |
| `/images/`       | `https://old.example.com/pic`    | `images/a/b.png`        | `https://old.example.com/pic/a/b.png`    |
| `/images/`       | `https://old.example.com/images` | `images/a/b.png`        | `https://old.example.com/images/a/b.png` |

When the prefix is the same as the origin directory, you still need to fill in **Directory**, as in the last row.

When multiple rules can match, the one with the longest prefix is used first. If that one doesn't produce a normal result, S4 tries the next matching rule.

## Requirements for the origin

| Item                   | Description                                                                                                          |
| :--------------------- | -------------------------------------------------------------------------------------------------------------------- |
| Request method         | The origin receives a GET request                                                                                    |
| Authentication         | The origin must allow anonymous reads, and custom request headers aren't supported                                   |
| Headers and parameters | The requester's headers, cookies, and URL parameters aren't forwarded                                                |
| User-Agent             | Begins with `@Bitiful/MirrorServer`. If the origin has UA or hotlink restrictions, allow it and allow empty Referers |
| Response               | 200 if the file exists, 404 if it doesn't                                                                            |
| Content-Type           | Saved to the bucket, so make sure the origin returns the correct type                                                |

Files whose names contain `?`, `#`, or `%` may not fetch correctly from the origin. Migrate them separately.

## How it interacts with other features

| Feature                           | Behavior                                                                                                                                                                   |
| :-------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Public read and signatures        | Permissions are checked first, and requests without read permission don't go to the origin                                                                                 |
| HEAD requests                     | Don't trigger an origin fetch. If the file isn't in the bucket, S4 returns 404 directly                                                                                    |
| Listing                           | Files that haven't been fetched yet don't appear in the file list                                                                                                          |
| Range requests                    | Supported. The bucket stores the complete file                                                                                                                             |
| Image processing                  | Processing parameters and Styles work from the first origin fetch                                                                                                          |
| Video and animation frame capture | The first request returns `202 MirrorFetching`. Request again after the file is saved                                                                                      |
| CDN                               | Access through a CDN domain also triggers an origin fetch                                                                                                                  |
| Delete                            | A file deleted from the bucket is fetched from the origin again the next time it's requested. To delete it for good, also delete the file at the origin or remove the rule |

Mirror back-to-origin is shared by all users at 1000 QPS. See [Limits](https://docs.bitiful.com/en/bitiful-s4/limits.md).

## Migration recommendations

### Configure origin fetch

Add a rule for the directory to migrate, and test with an address that you know exists at the origin.

### Switch your business over

Write new files directly to S4, and switch read addresses to S4 or a CDN that uses it as the origin.

### Fill in cold data

Files that are rarely accessed don't trigger an origin fetch. Use [Rclone](https://docs.bitiful.com/en/best-practice/solutions/rclone.md) to sync the remaining files.

### Delete the rule

After you verify the file counts match, delete the mirror rule, then take the origin offline.

## FAQ

<details>
<summary>It returns 424 MirrorFailed</summary>

The origin can't be reached, or it returned a status code other than 200 or 404. The error message states the status code the origin returned. Check the hostname, certificate, hotlink protection, and access restrictions.

</details>

<details>
<summary>It returns 404 NoSuchKey</summary>

The origin doesn't have this file, or no rule matched. Check the prefix and directory against the mapping table above.

</details>

<details>
<summary>I see "已经存在相同的前缀配置"</summary>

The message means "a configuration with the same prefix already exists". Delete the old rule first, then add it.

</details>

<details>
<summary>The origin updated the file, but the bucket still has the old one</summary>

Once a file is saved, S4 no longer goes back to the origin for it. Delete the file in the bucket, and the next request fetches it again.

</details>

<details>
<summary>Can it act only as a proxy, without saving to the bucket?</summary>

No. If you only need acceleration and don't need to save files, use the [CDN's custom origin](https://docs.bitiful.com/en/cdn/handbook/origin.md).

</details>
