No, it still worked. Cloudflare are aware of this for several years and havenât done anything to prevent it so I donât think its something they want to stop. Itâs possible by nature of their architecture, and could easily be stopped but they havenât so I think its allowed, though Iâm not sure.
Itâs not something we use in production but I have re-tested it this morning and can confirm it works.
Setup an A record gb.yourdomain.com
for the IP of one of Cloudflareâs United Kingdom datacenters, such as 8.41.7.4
which I confirmed working just now. You can use various IP tools to find all the IPâs for Cloudflareâs ASN in which location.
'strict mode';
export default {
async fetch(request) {
return fetch(request.url, {
body: request.body,
headers: request.headers,
method: request.method,
redirect: 'manual',
cf: {
resolveOverride: ['MAN','EDI','LHR'].includes(request.cf.colo)
? 'api.yourdomain.com'
: 'gb.yourdomain.com'
}
});
},
};
Make sure the
Cloud is on for both your API record.
To explain whats happening here. Essentially the HTTP request hits Cloudflares IP for the hostname api.yourdomain.com
which a Worker script is setup on. The worker then resolves the IP for API to instead be the IP of GB A Record which hits the datacenter in the desired geolocation and since itâs the same hostname (i.e. api.yourdomain.com) it invokes the same Worker script again but from another geolocation, it then hits the IP of your API server.
Hereâs the test, for me Iâm in China so the first request hits a datacenter in Hong Kong, and then the worker makes a second request to a datacenter in the United Kingdom. I will provide the logs for both requests below.
The first request:
{
"outcome": "ok",
"scriptName": "worker-weathered-glade-d581",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [],
"eventTimestamp": 1694839245034,
"event": {
"request": {
"url": "https://api.yourdomain.com/",
"method": "GET",
"headers": {
"accept": "*/*",
"accept-encoding": "gzip",
"cf-connecting-ip": "REDACTED",
"cf-ipcountry": "CN",
"cf-ray": "REDACTED",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"host": "api.yourdomain.com",
"user-agent": "curl/8.1.2",
"x-forwarded-proto": "https",
"x-real-ip": "REDACTED"
},
"cf": {
"clientTcpRtt": 76,
"longitude": "104.05550",
"latitude": "30.64980",
"tlsCipher": "AEAD-AES256-GCM-SHA384",
"continent": "AS",
"asn": 9808,
"country": "CN",
"tlsClientAuth": {
"certIssuerDNLegacy": "",
"certIssuerSKI": "",
"certSubjectDNRFC2253": "",
"certSubjectDNLegacy": "",
"certFingerprintSHA256": "",
"certNotBefore": "",
"certSKI": "",
"certSerial": "",
"certIssuerDN": "",
"certVerified": "NONE",
"certNotAfter": "",
"certSubjectDN": "",
"certPresented": "0",
"certRevoked": "0",
"certIssuerSerial": "",
"certIssuerDNRFC2253": "",
"certFingerprintSHA1": ""
},
"tlsExportedAuthenticator": {
"clientFinished": "6f1b5d862487a8e52cd205c10e0a612872663f5beacac5af8d7a0e018d7fd143864fae0faca91f93a1904b01efb8345b",
"clientHandshake": "21d13e08a59128ff45aca00b79c467151db3c200783a15cc71728283ae631494dfcf02b9e5e4776236132e2c6a7bf645",
"serverHandshake": "c1f3740dcda5d22dc31257e52c66c24422c29b37c5c42a7bef419088b7230fe59764ff4b9a4d2c2792b9f20efd325dbf",
"serverFinished": "bd114e5a9ba1ddd4e00132eefed2020942d88c80233f574a4874c7d1040c8b8964fd57e71363e22e57a66c4070eccca9"
},
"tlsVersion": "TLSv1.3",
"city": "Chengdu",
"timezone": "Asia/Shanghai",
"region": "Sichuan",
"requestPriority": "weight=16;exclusive=0;group=0;group-weight=0",
"colo": "HKG",
"httpProtocol": "HTTP/2",
"regionCode": "SC",
"asOrganization": "China Mobile",
"edgeRequestKeepAliveStatus": 1
}
},
"response": {
"status": 200
}
},
"id": 1
}
And the 2nd request (made by the Worker).
{
"outcome": "ok",
"scriptName": "worker-weathered-glade-d581",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [],
"eventTimestamp": 1694839486890,
"event": {
"request": {
"url": "https://api.yourdomain.com/",
"method": "GET",
"headers": {
"accept": "*/*",
"accept-encoding": "gzip",
"cf-connecting-ip": "162.158.178.8",
"cf-ipcountry": "HK",
"cf-ray": "REDACTED",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"host": "api.yourdomain.com",
"user-agent": "curl/8.1.2",
"x-forwarded-for": "REDACTED",
"x-forwarded-proto": "https",
"x-real-ip": "162.158.178.8"
},
"cf": {
"clientTcpRtt": 198,
"longitude": "114.17590",
"latitude": "22.28420",
"tlsCipher": "AEAD-AES128-GCM-SHA256",
"continent": "AS",
"asn": 13335,
"clientAcceptEncoding": "gzip",
"country": "HK",
"tlsClientAuth": {
"certIssuerDNLegacy": "",
"certIssuerSKI": "",
"certSubjectDNRFC2253": "",
"certSubjectDNLegacy": "",
"certFingerprintSHA256": "",
"certNotBefore": "",
"certSKI": "",
"certSerial": "",
"certIssuerDN": "",
"certVerified": "NONE",
"certNotAfter": "",
"certSubjectDN": "",
"certPresented": "0",
"certRevoked": "0",
"certIssuerSerial": "",
"certIssuerDNRFC2253": "",
"certFingerprintSHA1": ""
},
"tlsExportedAuthenticator": {
"clientFinished": "002886b97a97b7ad5e6804419197e5d080c4b8c1da9af19f39603e08caf92403",
"clientHandshake": "0aa5a5eb9cb79e1a0e61c08a4d32476f75e07eca2c7bfd47175a9daa85529e26",
"serverHandshake": "69f056495c64bef187dfe8647da8385d37e7e8123001cc781cf088b8e8753718",
"serverFinished": "2f1e2d2bb395132dfdd7fc0a65d08e9df2c333785f87353519d62872b90a08c4"
},
"tlsVersion": "TLSv1.3",
"city": "Hong Kong",
"timezone": "Asia/Hong_Kong",
"region": "Central and Western District",
"requestPriority": "weight=16;exclusive=0;group=0;group-weight=0",
"colo": "MAN",
"httpProtocol": "HTTP/2",
"regionCode": "HCW",
"asOrganization": "Cloudflare",
"edgeRequestKeepAliveStatus": 1
}
},
"response": {
"status": 200
}
},
"id": 0
}
Hope that helps. It might be nice if Cloudflare make an official guide rather than the community or developers having to figure this stuff out. Itâs not easy for a beginner to just think of this stuff since they donât have the big picture understanding of how everything works.