Publish Your Site With Cloudflare R2
Chinaski can publish your website to Cloudflare R2. R2 is a file storage service from Cloudflare. Your visitors load the site from Cloudflare, not from the computer where Chinaski runs. This means the computer running Chinaski does not need to be online for your site to work.
Every time you publish, Chinaski builds your site and uploads the files to R2.
This guide takes about 20 to 30 minutes.
Before you start
You need:
- Chinaski, installed and running. You must be able to open the admin area.
- A Cloudflare account. A free account is enough.
- A domain name, added to your Cloudflare account. For example,
example.com. - The web address you want to use for your site. For example,
www.example.comorblog.example.com. In this guide we usewww.example.com.
Important: Use a separate R2 bucket for each website. When Chinaski publishes, it makes the bucket match your site exactly. It deletes all other files in the bucket. Do not store other files in this bucket.
Step 1: Create a bucket
A bucket is a storage space for your site's files.
- Sign in to the Cloudflare dashboard at https://dash.cloudflare.com.
- In the menu on the left, select R2 Object Storage.
- Select Create bucket.
- Enter a name for the bucket. Use only lowercase letters, numbers and hyphens. For example:
www-example-com. - For the storage class, select Standard.
- Select Create bucket.
Write down the bucket name. You need it in Step 5.
Step 2: Create access keys
Chinaski needs keys to upload files to your bucket.
- Go to the R2 Object Storage overview page.
- Select Manage API tokens.
- Select Create API token. If Cloudflare asks which type of token you want, select Account API token.
- Enter a name for the token. For example:
Chinaski. - Under Permissions, select Object Read & Write.
- Under the bucket scope, select Apply to specific buckets only. Then select the bucket you created in Step 1.
- Select Create API token.
Cloudflare now shows your keys. Copy these two values and keep them in a safe place:
- Access Key ID
- Secret Access Key
Important: Cloudflare shows the Secret Access Key only once. If you lose it, delete the token and create a new one.
You also need your Account ID. You can find it on the R2 overview page. It is a long code of letters and numbers.
Step 3: Connect your domain to the bucket
This step makes your site public at your own web address.
- Go to R2 Object Storage and open your bucket.
- Select the Settings tab.
- Find Custom Domains and select Add (or Connect Domain).
- Enter your site's address. For example:
www.example.com. - Follow the steps on the screen to confirm.
Cloudflare creates the DNS record for you. You do not need to change your DNS records yourself.
After a few minutes, the status changes to Active. The bucket is now public at this address. You do not need to change any other public access setting.
Note: You may also see a setting called Public Development URL (
r2.dev). This address is only for testing. Do not use it for your real site. Leave it turned off.
Step 4: Add a URL rewrite rule
Your site's pages are saved as files called index.html. For example, the page www.example.com/about/ is saved as about/index.html.
R2 does not find these files automatically. Without this rule, visitors see an error on every page. The rule adds index.html to every address that ends with /.
In the Cloudflare dashboard, open your domain (for example,
example.com). Do not open R2.In the menu on the left, select Rules.
Create a new URL Rewrite Rule. Depending on your dashboard, select one of these:
- Rules > Overview > Create rule > URL Rewrite Rule
- Rules > Transform Rules > Rewrite URL > Create rule
Enter a name for the rule. For example:
Site index pages.Under If incoming requests match, select Custom filter expression.
Select Edit expression. Paste this text. Replace
www.example.comwith your site's address:(http.host eq "www.example.com" and ends_with(http.request.uri.path, "/"))Under Then, find Path. Select Rewrite to, then select Dynamic. Paste this text:
concat(http.request.uri.path, "index.html")For Query, keep Preserve.
Select Deploy.
Step 5: Set up Chinaski
Open the Chinaski admin area.
Go to Settings > General.
In Site URL, enter your site's full address. Include
https://. Do not add/at the end. For example:https://www.example.comSave.
Go to Settings > Deploy.
Fill in these fields:
Field What to enter Enable S3 deploy On Bucket Your bucket name from Step 1. For example: www-example-comEndpoint https://<ACCOUNT_ID>.r2.cloudflarestorage.com, replacing<ACCOUNT_ID>with your Account ID from Step 2Region autoAccess Key Your Access Key ID from Step 2 Secret Key Your Secret Access Key from Step 2 Deploy automatically after a successful build On Select Save Deploy Settings.
Important: The Endpoint must end with
.r2.cloudflarestorage.com. Do not add your bucket name at the end.Cloudflare sometimes shows the address with the bucket name at the end, like this:
https://<ACCOUNT_ID>.r2.cloudflarestorage.com/<BUCKET_NAME>If you copy that address, remove the last
/and the bucket name. If you do not, Chinaski puts your site in a folder inside the bucket, and visitors see an error on every page.
Step 6: Test and publish
- On the Settings > Deploy page, select Test S3 Connection. You should see a success message. If you see an error, check the Bucket, Endpoint and keys again.
- Publish a page, or edit a page and save it.
- Wait for the build and upload to finish. The status message at the top of the admin area shows the progress.
- Open your site in a web browser. For example:
https://www.example.com/
Check these addresses:
- Your home page:
https://www.example.com/ - A page inside the site. For example:
https://www.example.com/about/ - The sitemap:
https://www.example.com/sitemap.xml
If all of them load, your site is published.
From now on, Chinaski uploads your changes every time you publish. If an upload fails, the previous version of your site stays online. Fix the problem and publish again.
Troubleshooting
Every page shows "Error 404" and "Is this your bucket?"
This message comes from R2. It means your domain works, but R2 cannot find the file.
Check these things:
- Your files are in a folder. In Cloudflare, open your bucket and select the Objects tab. The file
index.htmlmust be at the top level, not inside a folder. If your files are inside a folder with your bucket's name, your Endpoint has the bucket name at the end. Correct it (see Step 5) and publish again. Chinaski removes the old folder automatically. - The domain is connected to a different bucket. Open the bucket that contains your files. Select Settings and check that your site's address is listed under Custom Domains.
- The bucket is empty. Check that Enable S3 deploy and Deploy automatically after a successful build are both on. Then publish again.
The home page works, but other pages show an error
The URL rewrite rule is missing or incorrect. Check Step 4. Make sure the rule is turned on, and that the address in the rule is exactly the same as your site's address.
Pages ending in index.html show an error, but only after adding the rule
The rule's filter is too broad. It must apply only to addresses that end with /. Check the expression in Step 4, instruction 6.
"Test S3 Connection" fails
- Check that the Endpoint is exactly
https://<ACCOUNT_ID>.r2.cloudflarestorage.com, with your own Account ID. - Check that the Bucket name is correct. It must not contain
/. - Check that you copied both keys completely.
- Check that the API token has Object Read & Write permission for this bucket.
Links on the site point to the wrong address
Check Site URL in Settings > General. It must be your public address, for example https://www.example.com. Then publish again.
Limits
- Forms and document downloads: your site is served from R2 only, so R2 cannot send form submissions to Chinaski. If the computer running Chinaski cannot be reached from the internet, contact forms and protected document downloads do not work.
- Costs: Cloudflare R2 has a free tier. At the time of writing, it includes 10 GB of storage per month, 1 million upload operations and 10 million read operations per month. Cloudflare does not charge for data sent to your visitors. If you use more than this, Cloudflare charges you. Check the current prices on the Cloudflare R2 pricing page. The free tier applies only to Standard storage.