2017-04-01 21:44:35 +08:00
---
name: Cloudflare
2017-10-11 20:33:17 +08:00
title: Cloudflare Provider
2017-04-01 21:44:35 +08:00
layout: default
jsId: CLOUDFLAREAPI
---
# Cloudflare Provider
2019-05-18 23:08:18 +08:00
## Important notes
* When using `SPF()` or the `SPF_BUILDER()` the records are converted to RecordType `TXT` as Cloudflare API fails otherwise. See more [here ](https://github.com/StackExchange/dnscontrol/issues/446 ).
2017-04-01 21:44:35 +08:00
## Configuration
2019-10-23 23:48:00 +08:00
In the credentials file you must provide a [Cloudflare API token ](https://dash.cloudflare.com/profile/api-tokens ):
{% highlight json %}
{
"cloudflare": {
"apitoken": "your-cloudflare-api-token"
}
}
{% endhighlight %}
2020-03-16 23:25:20 +08:00
Make sure the token has at least the right read zones and edit DNS records (i.e. `Zone → Zone → Read` and `Zone → DNS → Edit` ; to modify Page Rules additionally requires `Zone → Page Rules → Edit` );
2019-10-23 23:48:00 +08:00
checkout [Cloudflare's documentation ](https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys ) for instructions on how to generate and configure permissions on API tokens.
2019-10-24 01:27:04 +08:00
Or you can provide your Cloudflare API username and access key instead (but it isn't recommended because those credentials give DNSControl access to the complete Cloudflare API):
2017-04-01 21:44:35 +08:00
{% highlight json %}
{
2017-10-11 20:33:52 +08:00
"cloudflare": {
2017-04-01 21:44:35 +08:00
"apikey": "your-cloudflare-api-key",
2017-04-20 00:41:12 +08:00
"apiuser": "your-cloudflare-email-address"
2017-04-01 21:44:35 +08:00
}
}
{% endhighlight %}
2018-12-19 22:48:27 +08:00
If your Cloudflare account has access to multiple Cloudflare accounts, you can specify which Cloudflare account should be used when adding new domains:
{% highlight json %}
{
"cloudflare": {
2019-10-23 23:48:00 +08:00
"apitoken": "...",
2018-12-19 22:48:27 +08:00
"accountid": "your-cloudflare-account-id",
"accountname": "your-cloudflare-account-name"
}
}
{% endhighlight %}
2017-04-01 21:44:35 +08:00
## Metadata
2019-05-12 09:32:52 +08:00
Record level metadata available:
2017-05-20 02:15:57 +08:00
* `cloudflare_proxy` ("on", "off", or "full")
2017-04-01 21:44:35 +08:00
2019-05-12 09:32:52 +08:00
Domain level metadata available:
2017-05-20 02:15:57 +08:00
* `cloudflare_proxy_default` ("on", "off", or "full")
2019-06-13 19:32:54 +08:00
* `cloudflare_universalssl` (unset to keep untouched; otherwise "on, or "off")
2017-04-01 21:44:35 +08:00
2019-05-12 09:32:52 +08:00
Provider level metadata available:
2017-05-20 02:15:57 +08:00
* `ip_conversions`
* `manage_redirects` : set to `true` to manage page-rule based redirects
2017-04-01 21:44:35 +08:00
2017-05-17 03:55:51 +08:00
What does on/off/full mean?
2017-05-11 13:02:57 +08:00
2017-05-17 03:55:51 +08:00
* "off" disables the Cloudflare proxy
* "on" enables the Cloudflare proxy (turns on the "orange cloud")
* "full" is the same as "on" but also enables Railgun. DNSControl will prevent you from accidentally enabling "full" on a CNAME that points to an A record that is set to "off", as this is generally not desired.
2019-05-21 10:33:50 +08:00
Good to know: You can also set the default proxy mode using `DEFAULTS()` function, see:
{% highlight js %}
DEFAULTS(
CF_PROXY_DEFAULT_OFF // turn proxy off when not specified otherwise
);
{% endhighlight %}
2017-05-17 03:55:51 +08:00
**Aliases:**
2017-05-11 13:02:57 +08:00
2017-10-11 21:55:57 +08:00
To make configuration files more readable and less prone to errors,
2019-05-21 10:33:50 +08:00
the following aliases are *pre-defined* :
2017-05-11 13:02:57 +08:00
2017-10-11 21:55:57 +08:00
{% highlight js %}
2017-05-17 03:55:51 +08:00
// Meta settings for individual records.
var CF_PROXY_OFF = {'cloudflare_proxy': 'off'}; // Proxy disabled.
var CF_PROXY_ON = {'cloudflare_proxy': 'on'}; // Proxy enabled.
var CF_PROXY_FULL = {'cloudflare_proxy': 'full'}; // Proxy+Railgun enabled.
// Per-domain meta settings:
// Proxy default off for entire domain (the default):
var CF_PROXY_DEFAULT_OFF = {'cloudflare_proxy_default': 'off'};
// Proxy default on for entire domain:
var CF_PROXY_DEFAULT_ON = {'cloudflare_proxy_default': 'on'};
2019-06-13 19:32:54 +08:00
// UniversalSSL off for entire domain:
var CF_UNIVERSALSSL_OFF = { cloudflare_universalssl: 'off' };
// UniversalSSL on for entire domain:
var CF_UNIVERSALSSL_ON = { cloudflare_universalssl: 'on' };
2017-05-11 13:02:57 +08:00
{% endhighlight %}
2017-05-17 03:55:51 +08:00
The following example shows how to set meta variables with and without aliases:
2017-05-11 13:02:57 +08:00
2017-10-11 21:55:57 +08:00
{% highlight js %}
2017-10-11 20:33:52 +08:00
D('example.tld', REG_NONE, DnsProvider(CLOUDFLARE),
A('www1','1.2.3.11', CF_PROXY_ON), // turn proxy ON.
A('www2','1.2.3.12', CF_PROXY_OFF), // default is OFF, this is a no-op.
2017-05-17 03:55:51 +08:00
A('www3','1.2.3.13', {'cloudflare_proxy': 'on'}) // why would anyone do this?
2017-05-11 13:02:57 +08:00
);
{% endhighlight %}
2017-04-01 21:44:35 +08:00
## Usage
2017-10-11 21:55:57 +08:00
Example Javascript:
2017-04-01 21:44:35 +08:00
{% highlight js %}
2017-10-11 20:33:52 +08:00
var REG_NONE = NewRegistrar('none', 'NONE')
var CLOUDFLARE = NewDnsProvider('cloudflare','CLOUDFLAREAPI');
2017-05-17 03:55:51 +08:00
// Example domain where the CF proxy abides by the default (off).
2017-10-11 20:33:52 +08:00
D('example.tld', REG_NONE, DnsProvider(CLOUDFLARE),
2017-05-17 03:55:51 +08:00
A('proxied','1.2.3.4', CF_PROXY_ON),
A('notproxied','1.2.3.5'),
A('another','1.2.3.6', CF_PROXY_ON),
ALIAS('@','www.example.tld.', CF_PROXY_ON),
CNAME('myalias','www.example.tld.', CF_PROXY_ON)
);
2017-04-01 21:44:35 +08:00
2017-05-17 03:55:51 +08:00
// Example domain where the CF proxy default is set to "on":
2017-10-11 20:33:52 +08:00
D('example2.tld', REG_NONE, DnsProvider(CLOUDFLARE),
2017-05-17 03:55:51 +08:00
CF_PROXY_DEFAULT_ON, // Enable CF proxy for all items unless otherwise noted.
A('proxied','1.2.3.4'),
A('notproxied','1.2.3.5', CF_PROXY_OFF),
A('another','1.2.3.6'),
ALIAS('@','www.example2.tld.'),
CNAME('myalias','www.example2.tld.')
2017-04-01 21:44:35 +08:00
);
{%endhighlight%}
## Activation
DNSControl depends on a Cloudflare Global API Key that's available under "My Settings".
2017-05-03 21:32:47 +08:00
## New domains
2019-05-23 21:29:21 +08:00
If a domain does not exist in your Cloudflare account, DNSControl
2017-05-03 21:32:47 +08:00
will *not* automatically add it. You'll need to do that via the
2017-05-17 03:55:51 +08:00
control panel manually or via the `dnscontrol create-domains` command.
2017-05-20 02:15:57 +08:00
## Redirects
2020-03-16 23:25:20 +08:00
The Cloudflare provider can manage "Forwarding URL" Page Rules (redirects) for your domains. Simply use the `CF_REDIRECT` and `CF_TEMP_REDIRECT` functions to make redirects:
2017-05-20 02:15:57 +08:00
{% highlight js %}
// chiphacker.com is an alias for electronics.stackexchange.com
2017-10-11 20:33:52 +08:00
var CLOUDFLARE = NewDnsProvider('cloudflare','CLOUDFLAREAPI', {"manage_redirects": true}); // enable manage_redirects
2017-08-03 01:51:13 +08:00
2017-10-11 20:33:52 +08:00
D("chiphacker.com", REG_NONE, DnsProvider(CLOUDFLARE),
2017-05-20 02:15:57 +08:00
// must have A records with orange cloud on. Otherwise page rule will never run.
A("@","1.2.3.4", CF_PROXY_ON),
A("www", "1.2.3.4", CF_PROXY_ON)
A("meta", "1.2.3.4", CF_PROXY_ON),
// 302 for meta subdomain
2017-08-03 01:51:13 +08:00
CF_TEMP_REDIRECT("meta.chiphacker.com/*", "https://electronics.meta.stackexchange.com/$1"),
2017-05-20 02:15:57 +08:00
// 301 all subdomains and preserve path
2017-08-03 01:51:13 +08:00
CF_REDIRECT("*chiphacker.com/*", "https://electronics.stackexchange.com/$2"),
2017-05-20 02:15:57 +08:00
);
{%endhighlight%}
Notice a few details:
2017-08-30 01:49:39 +08:00
1. We need an A record with cloudflare proxy on, or the page rule will never run.
2017-05-20 02:15:57 +08:00
2. The IP address in those A records may be mostly irrelevant, as cloudflare should handle all requests (assuming some page rule matches).
2017-05-26 08:38:48 +08:00
3. Ordering matters for priority. CF_REDIRECT records will be added in the order they appear in your js. So put catch-alls at the bottom.
2020-03-16 23:25:20 +08:00
4. if _any_ `CF_REDIRECT` or `CF_TEMP_REDIRECT` functions are used then `dnscontrol` will manage _all_ "Forwarding URL" type Page Rules for the domain. Page Rule types other than "Forwarding URL” will be left alone.