Uçta Coğrafi Yönlendirme: İçerik Kişiselleştirme ve GDPR Uyumluluğu
Ülkeye, bölgeye ve dile göre yönlendirme mantığı oluşturmayı öğrenin Coğrafi sınırlama ve yerelleştirilmiş fiyatlara ilişkin örneklerle doğrudan İşçiler bölümünde ve ana sunucuyu değiştirmeden GDPR uyumluluğu.
Coğrafi Yönlendirme Neden Uç Noktaya Ait?
İçeriği coğrafi konuma göre kişiselleştirmek, Küresel web uygulamalarında en yaygın ihtiyaçlar: fiyatların yerel para birimi, ülkeye özgü düzenlemelere (Avrupa'da GDPR, Kaliforniya'daki CCPA), belirli yargı alanlarındaki içeriği engelleyin ve yönlendirin bölgesel alanlara yöneliktir.
Geçmişte bu, sunucu tarafı coğrafi konum veritabanlarıyla ele alınıyordu. (MaxMind GeoLite2) veya manuel olarak yapılandırılmış CDN kurallarıyla. Her iki yaklaşım da sınırlamaları vardır: veritabanı sunucusu gecikme ekler, CDN kuralları statiktir ve dinamik olarak güncellenmesi zordur.
Cloudflare Workers ile coğrafi konum zaten mevcut
nesnede request.cf bakımı gereken herhangi bir veritabanı olmadan.
Cloudflare, ağın BGP topolojisine göre konumu belirler.
IP aramasında değil; ülke düzeyinde doğruluk oranı %99,9'un üzerindedir.
Ne Öğreneceksiniz
- Mevcut özellikler
request.cfcoğrafi konum için - Coğrafi sınırlama: ülkeye göre engelleme ve yönlendirme
- Yerelleştirilmiş fiyatlandırma: Bölgeye göre para birimi ve KDV
- GDPR uyumluluğu: AB kullanıcıları için otomatik çerez onayı
- Özel başlıklarla çok bölgeli yönlendirme
- Dağıtım olmadan coğrafi tabanlı mantığı test etme
Nesne request.cf Cloudflare tarafından
Bir Cloudflare Çalışanına yapılan her talep, konuyu içerir cf ile
Cloudflare tarafından gerçek zamanlı olarak belirlenen coğrafi ve ağ meta verileri:
// Tutte le proprieta disponibili in request.cf
export default {
async fetch(request: Request): Promise<Response> {
const cf = request.cf as CfProperties;
// Geolocalizzazione
const country = cf.country; // "IT" - ISO 3166-1 alpha-2
const region = cf.region; // "Puglia" - nome della regione
const regionCode = cf.regionCode; // "75" - codice regione
const city = cf.city; // "Bari"
const postalCode = cf.postalCode; // "70121"
const latitude = cf.latitude; // "41.1171"
const longitude = cf.longitude; // "16.8719"
const timezone = cf.timezone; // "Europe/Rome"
const continent = cf.continent; // "EU"
// Rete
const asn = cf.asn; // 1234 - Autonomous System Number
const asOrganization = cf.asOrganization; // "Telecom Italia"
const isEuCountry = cf.isEUCountry; // "1" o "0"
// Performance
const colo = cf.colo; // "FCO" - datacenter Cloudflare piu vicino
const httpProtocol = cf.httpProtocol; // "HTTP/2"
const tlsVersion = cf.tlsVersion; // "TLSv1.3"
return Response.json({
country,
region,
city,
timezone,
continent,
isEuCountry,
colo,
});
},
};
// Tipo per le proprieta cf (parziale)
interface CfProperties {
country?: string;
region?: string;
regionCode?: string;
city?: string;
postalCode?: string;
latitude?: string;
longitude?: string;
timezone?: string;
continent?: string;
asn?: number;
asOrganization?: string;
isEUCountry?: string;
colo?: string;
httpProtocol?: string;
tlsVersion?: string;
}
Coğrafi Eskrim: Ülkeye göre engelleme
Coğrafi sınırlama, içeriğin belirli bir noktaya engellenmesi veya yeniden yönlendirilmesi modelidir. ülkeler. En yaygın kullanım durumları şunlardır: uluslararası yaptırımlar için engelleme, Bölgesel lisanslara sahip içerik (yayın akışı, medya) ve henüz onaylanmamış pazarlar belirli ürünlere açıktır:
// src/geo-fence-worker.ts
// Paesi con accesso bloccato (esempio: sanzioni, licenze)
const BLOCKED_COUNTRIES = new Set(['KP', 'IR', 'SY', 'CU']);
// Paesi che richiedono un redirect a una versione localizzata
const REDIRECTS: Record<string, string> = {
DE: 'https://de.example.com',
FR: 'https://fr.example.com',
JP: 'https://jp.example.com',
};
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const cf = request.cf as CfProperties;
const country = cf.country ?? 'US';
const url = new URL(request.url);
// Blocco per paesi non consentiti
if (BLOCKED_COUNTRIES.has(country)) {
return new Response(
JSON.stringify({
error: 'Service not available in your region',
country,
}),
{
status: 451, // 451 Unavailable For Legal Reasons
headers: {
'Content-Type': 'application/json',
'Vary': 'CF-IPCountry',
},
}
);
}
// Redirect verso versione localizzata per certi paesi
const redirectTarget = REDIRECTS[country];
if (redirectTarget && !url.pathname.startsWith('/api/')) {
const targetUrl = new URL(url.pathname + url.search, redirectTarget);
return Response.redirect(targetUrl.toString(), 302);
}
// Aggiunge header con il paese per il downstream (server di origine)
const headers = new Headers(request.headers);
headers.set('CF-Worker-Country', country);
headers.set('CF-Worker-Continent', cf.continent ?? '');
headers.set('CF-Worker-Timezone', cf.timezone ?? '');
// Prosegui verso il server di origine
return fetch(new Request(request.url, {
method: request.method,
headers,
body: ['GET', 'HEAD'].includes(request.method) ? undefined : request.body,
}));
},
};
interface CfProperties {
country?: string;
continent?: string;
timezone?: string;
}
interface Env {}
Yerelleştirilmiş Fiyatlar ve Para Birimleri
Fiyatları müşterinin yerel para biriminde göstermek en iyi uygulamadır Dönüşüm oranını artıran e-ticaret. İşçi ile şunları yapabilirsiniz: istek ulaşmadan önce doğru para birimini belirleyin kaynak sunucu:
// src/pricing-worker.ts - prezzi localizzati all'edge
interface CurrencyConfig {
code: string;
symbol: string;
position: 'before' | 'after';
vatRate: number; // IVA in percentuale (0.22 = 22%)
}
const COUNTRY_CURRENCY: Record<string, CurrencyConfig> = {
// Eurozona
IT: { code: 'EUR', symbol: '€', position: 'before', vatRate: 0.22 },
DE: { code: 'EUR', symbol: '€', position: 'before', vatRate: 0.19 },
FR: { code: 'EUR', symbol: '€', position: 'before', vatRate: 0.20 },
ES: { code: 'EUR', symbol: '€', position: 'before', vatRate: 0.21 },
// Altre valute
GB: { code: 'GBP', symbol: '£', position: 'before', vatRate: 0.20 },
US: { code: 'USD', symbol: '






