It will take you about 10 minutes to study this document. By studying this document, you can understand:
1. Why is origin-pull based on client's geo location necessary?
2. Use cases for origin-pull based on client's geo location.
3. The specific steps for implementing origin-pull based on client's geo location with EdgeOne edge functions and rule engines.
Background
In today's global business environment, enterprises must provide services that transcend geographic boundaries, ensuring a consistent experience for users regardless of their location. Global service demands require enterprises to respond quickly to the needs of users in different regions, providing personalized content and services. Additionally, with the increasing stringency of data protection regulations worldwide, such as the European Union's General Data Protection Regulation (GDPR), enterprises must ensure compliance when handling user data to avoid legal risks and reputational damage.
Through EdgeOne edge functions and rule engines, a solution is implemented that routes users' requests to the nearest specified origin server based on their geo locations. This can address the aforementioned challenges by analyzing users' geo locations and network requests, and routing the requests to the specified optimal origin server. Specifically, the edge function defines the origin-pull request header based on the client's geo location, and the rule engine uses this header to route the request to the specified origin server. This solution not only improves response speed and performance but also ensures compliance with data processing regulations. No matter where the user is in the world, they can enjoy fast and regulation-compliant service experience, helping your enterprise maintain competitiveness in the global market.
Use Cases
EdgeOne distributes requests from global users' clients to the specified origin server based on their geo locations. The following are specific application scenarios:
Enterprise internationalization: Enterprises can provide tailored services globally. For example, in the financial service sector, it ensures that transaction requests are quickly and accurately routed to the nearest server, reducing latency, and simultaneously provides region-specific investment recommendations and market analysis to meet the needs of users in different regions. Additionally, cross-border e-commerce platforms can locate users' geo locations to offer localized products and services, optimizing inventory and logistics policies to enhance user experience and operational efficiency.
Data privacy compliance: As data protection regulations become increasingly stringent, enterprises need to ensure their data processing activities comply with regulations corresponding to users' geo locations. Enterprises can route data requests to servers that comply with local data protection regulations, ensuring data compliance. This not only helps enterprises avoid legal risks but also respects user privacy, building user trust and laying a solid foundation for the enterprise's sustainable development.
Scenario
Assume that you are a technical lead of a global e-commerce platform and you have integrated your site domain name example.com
into EdgeOne. Your goal is to optimize the website's access speed and user experience, ensuring that global users can quickly access website content. To achieve this goal, you plan to dynamically route requests to the nearest origin server based on users' geo locations while ensuring that data processing activities comply with data protection regulations in the users' respective regions.
In this scenario, you have set two client regions and two corresponding origin server groups:
Chinese mainland client: For users from the Chinese mainland, you intend to route their requests to the origin server group located in the Chinese mainland. This ensures that the data is processed locally, reducing data transmission latency and improving access speed. This also helps ensure that data processing activities comply with Chinese mainland data protection regulations, protecting user privacy and avoiding legal risks. Requests routed to the Chinese mainland origin server will have an origin-pull request header X-Forwarded-For-Origin:cn
added by the edge function.
Singapore client: For users from Singapore (representing regions outside the Chinese mainland), you intend to route their requests to the origin server group located in Singapore. By leveraging the geographic advantage to reduce latency, user experience is enhanced while ensuring data processing activities comply with Singapore data protection regulations, maintaining user data privacy security. Requests routed to the Singapore origin server will have an origin-pull request header X-Forwarded-For-Origin:sg
added by the edge function.
Directions
Step 1: Connecting to EdgeOne
Refer to Quick Start to complete site connection and domain name connection. Step 2: Creating and Configuring the Edge Function
1. Log in to the EdgeOne console, select the site to be configured from the Site List, and enter the site management submenu. 2. In the left navigation bar, click Edge Functions > Function Management.
3. On the Function Management page, click Create function.
4. On the template selection page, select Create Hello World, and then click Next.
5. On the function creation page, enter the function name, description, and code. Below is sample code for origin-pull based on client's geo location:
const ROUTE_CLIENT_ORIGIN_MAP = {
'example.com': {
CN: 'cn',
_DEFAULT_: 'sg',
},
};
const ORIGIN_HEADER_NAME = 'X-Forwarded-For-Origin';
const OVERSEAS_AREA = '!CN';
const DEFAULT_AREA = '_DEFAULT_';
addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event));
});
async function handleEvent(event) {
const { request } = event;
request.headers.delete(ORIGIN_HEADER_NAME);
let host = request.headers.get('host');
let countryCodeAlpha2 = request.eo.geo?.countryCodeAlpha2;
const clientOriginMap = ROUTE_CLIENT_ORIGIN_MAP[host];
if (clientOriginMap) {
const originName = clientOriginMap[countryCodeAlpha2];
if (originName) {
request.headers.set(ORIGIN_HEADER_NAME, originName);
} else if (clientOriginMap[OVERSEAS_AREA]) {
request.headers.set(ORIGIN_HEADER_NAME, clientOriginMap[OVERSEAS_AREA]);
} else if (clientOriginMap[DEFAULT_AREA]) {
request.headers.set(ORIGIN_HEADER_NAME, clientOriginMap[DEFAULT_AREA]);
}
}
return;
}
Step 3: Configuring and Deploying Trigger Rules for the Edge Function
1. After editing the function, click Create and deploy. Once the function is deployed, you can directly click Add triggering rule to configure the trigger rules for the function.
2. In the function trigger rules, configure the trigger conditions for the function. Based on the current scenario, you can configure multiple trigger conditions using AND logic.
Here, configure the request HOST as example.com
.
When the request URL meets the above conditions, the edge function in Step 2 will be triggered, implementing origin-pull based on client's geo location.
3. Click OK to activate the trigger rules.
Step 4: Configuring the Rule Engine
1. Log in to the EdgeOne console, select the site to be configured from the Site List, and enter the site management submenu. 2. In the left navigation bar, click Site Acceleration to enter the global configuration page for the site. Then, click the Rule Engine tab.
3. On the Rule Engine page, click Create rule, and then select Add blank rule.
4. On the rule editing page, select HOST as the matching type to match requests for a specific domain name.
Here, configure the request HOST as example.com
.
5. On the rule editing page, click +IF, and based on the request header values in edge function code, configure different origin server groups.
Here, configure that when the HTTP request header X-Forwarded-For-Origin
equals cn
, the request will be forwarded to the origin server group in the Chinese mainland for processing; when the HTTP request header X-Forwarded-For-Origin
equals sg
, the request will be forwarded to the origin server group in Singapore for processing.
6. Click Save and publish to activate the rule engine.
Step 5: Verifying the Deployment Effect
To verify the validity status of edge functions, you can use the following methods:
In the Mac/Linux environment, to test the Google Chrome browser, you can run the command in the terminal: curl --user-agent "Chrome" https://example.com
For requests with the geo location of CN, edge functions will forward the requests to the origin server group in the Chinese mainland:
For requests with geo locations other than CN, edge functions will forward the requests to the origin server group in Singapore:
Visit the test address in the Google Chrome browser: https://example.com
For requests with the geo location of CN, edge functions will forward the requests to the origin server group in the Chinese mainland:
For requests with geo locations other than CN, edge functions will forward the requests to the origin server group in Singapore:
More Information
Was this page helpful?