# Connect CDN and WebP optimization

Connect CDN for free and get unlimited WebP image optimization for 20$ per month. WebP compression helps to make your store faster reducing image size up to 50%. Visit <https://www.cloudflare.com/> for more details.

{% hint style="info" %}
Throughout the guide replace "yourdomain.com" with the domain you are connecting to Cloudflare.
{% endhint %}

## Setup for a domain hosted on Cloudflare

1. Open a [Cloudflare](https://www.cloudflare.com/) account and attach billing information
2. [Migrate DNS records to Cloudflare ](https://support.cloudflare.com/hc/en-us/articles/200168856-Importing-and-exporting-DNS-records)if the domain is not already there.
3. [Switch NS records](https://support.cloudflare.com/hc/en-us/articles/205195708-Changing-your-domain-nameservers-to-Cloudflare) in the register if the domain is not already there.
4. Point [www.yourdomain.com](http://www.yourdomain.com) CNAME (proxy -> Yes) to
   * `lb.ireland.eu.i.readymage.com` (for instance created in EU Ireland region)&#x20;
   * `lb.ohio.us.i.readymage.com` (for instance created in US Ohio region),&#x20;
   * `lb.stockholm.eu.i.readymage.com` (for instance created in Stockholm region),&#x20;
   * `lb.central.ca.i.readymage.com` (for instance created in Canada Central region),&#x20;
   * `lb.central.me.i.readymage.com` (for instance created in Middle East UAE region),&#x20;
   * `lb.sydney.ap.i.readymage.com` (for instance created in Asia Pacific Sydney region)
5. Point yourdomain.com CNAME (proxy -> Yes) to&#x20;
   * `lb.ireland.eu.i.readymage.com` (for instance created in EU Ireland region)
   * `lb.ohio.us.i.readymage.com` (for instance created in US Ohio region),&#x20;
   * `lb.stockholm.eu.i.readymage.com` (for instance created in Stockholm region),&#x20;
   * `lb.central.ca.i.readymage.com` (for instance created in Canada Central region),
   * `lb.central.me.i.readymage.com` (for instance created in Middle East UAE region),&#x20;
   * `lb.sydney.ap.i.readymage.com` (for instance created in Asia Pacific Sydney region)
6. Adjust Cloudflare settings according to recommended settings.

## Setup for a domain hosted outside of Cloudflare

Cloudflare setup if you host your domain outside of Cloudflare and can't migrate NS records:

1. Open a [Cloudflare](https://www.cloudflare.com/) account and attach billing information
2. Purchase Cloudflare Business Plan for 200$ for the specific domain.
3. Contact Cloudflare support asking to switch the account using TXT (CNAME) validation method. Cloudflare performs the switch within hours.
4. Cloudflare support will provide you with the DNS record that you need to add.
5. Add the provided DNS record to your existing DNS hosting panel and wait for Cloudflare activation.
6. In Cloudflare point [www.yourdomain.com](http://www.yourdomain.com) CNAME (proxy -> Yes) to
   * `lb.ireland.eu.i.readymage.com` (for instance created in EU Ireland region)&#x20;
   * `lb.ohio.us.i.readymage.com` (for instance created in US Ohio region),&#x20;
   * `lb.stockholm.eu.i.readymage.com` (for instance created in Stockholm region),&#x20;
   * `lb.central.ca.i.readymage.com` (for instance created in Canada Central region),&#x20;
   * `lb.central.me.i.readymage.com` (for instance created in Middle East UAE region),&#x20;
   * `lb.sydney.ap.i.readymage.com` (for instance created in Asia Pacific Sydney region)
7. In Cloudflare, point yourdomain.com CNAME (proxy -> Yes) to&#x20;
   * `lb.ireland.eu.i.readymage.com` (for instance created in EU Ireland region)
   * `lb.ohio.us.i.readymage.com` (for instance created in US Ohio region),&#x20;
   * `lb.stockholm.eu.i.readymage.com` (for instance created in Stockholm region),&#x20;
   * `lb.central.ca.i.readymage.com` (for instance created in Canada Central region),
   * `lb.central.me.i.readymage.com` (for instance created in Middle East UAE region),&#x20;
   * `lb.sydney.ap.i.readymage.com` (for instance created in Asia Pacific Sydney region)
8. In existing DNS hosting panel point: [www.yourdomain.com](http://www.yourdomain.com) CNAME [www.yourdomain.com.cdn.cloudflare.net](http://www.yourdomain.com.cdn.cloudflare.net)
9. In existing DNS hosting panel point: yourdomain.com CNAME yourdomain.com.cdn.cloudflare.net
10. Adjust Cloudflare settings according to recommended settings:

## Recommended Cloudflare settings

### SSL/TLS

#### Edge Certificates

* Always Use HTTPS → On
* HSTS
  * Enable HSTS → On
  * Max Age Header → 6 months
  * Apply HSTS policy to subdomains → Off
  * Preload → On
  * No-Sniff Header → On
* Minimum TLS Version → TLS 1.2
* Opportunistic Encryption → On
* TLS 1.3 → On
* Automatic HTTPS Rewrites → On

### Security

#### WAF → Managed rules

* Managed rules → On
* Cloudflare Managed Ruleset
  * Cloudflare Magento → On
  * Cloudflare Php → On

#### Bots → Configure Super Bot Fight Mode

* JavaScript Detections → Off (if you leave it "on", the performance will decrease slightly, but it will be possible to detect robots more accurately)

#### Settings

* Browser Integrity Check → On
* Privacy Pass Support → On

### Speed

#### Optimization

* Polish → Lossy + webP
* Auto Minify → JavaScript, CSS, HTML
* Brotli → On
* Early Hints → On
* Enhanced HTTP/2 Prioritization → On
* Mirage → On
* Rocket Loader → Off (You can try to enable it for sites that are not single-page applications \[SPA], for example, for ScandiPWA which is a SPA site it should be disabled)

### Caching

#### Configuration

* Crawler Hints → On

#### Cache Rules → Create Rule

If incoming requests match: `Custom filter expression`

When incoming requests match

* Field: `URI Path`, Operator: `Starts with`, Value: `/static`
* Field: `URI Path`, Operator: `Starts with`, Value: `/media`

﻿Cache eligibility - `Eligible for cache.`

#### Edge TTL

* Ignore cache-control header and use this TTL
  * Input time-to-live (TTL) - `1 year`
* Status code TTL
  * Scope: `Range`, From - To: `300` `403`, Duration: `2 hours`
  * Scope: `Single code`, Status code: `404`, Duration: `No cache`
  * Scope: `Range`, From: `405` `509`, Duration: `2 hours`

#### Browser TTL

* Override orign and use this TTL
  * Input time-to-live (TTL) - `2 hours`

### Network

* HTTP/2 → On
* HTTP/2 to Origin → On
* HTTP/3 (with QUIC) → On
* 0-RTT Connection Resumption → On

## Exclude Magento admin from Cloudflare

In order to avoid Cloudflare becoming a bottleneck for long backend operations, you can exclude it from Cloudflare.

{% hint style="info" %}
Replace admin.yourdomain.com with your Magento 2 admin URL.
{% endhint %}

1. Set up a separate domain for your Magento 2 admin that differs from your Magento 2 front-end domain.
2. Point admin.yourdomain.com CNAME (proxy -> No) to&#x20;
   * `lb.ireland.eu.i.readymage.com` (for instance created in EU Ireland region)
   * `lb.ohio.us.i.readymage.com` (for instance created in US Ohio region),&#x20;
   * `lb.stockholm.eu.i.readymage.com` (for instance created in Stockholm region),&#x20;
   * `lb.central.ca.i.readymage.com` (for instance created in Canada Central region),
   * `lb.central.me.i.readymage.com` (for instance created in Middle East UAE region),&#x20;
   * `lb.sydney.ap.i.readymage.com` (for instance created in Asia Pacific Sydney region)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.readymage.com/project-development/connect-cdn-and-webp-optimization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
