From cfbaca8d6c0fe2f3fdb23005a37c07f483d934f3 Mon Sep 17 00:00:00 2001
From: Yanzii99 <153089451@qq.com>
Date: Fri, 28 Mar 2025 13:59:53 +0800
Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E4=BD=BF=E7=94=A8=20CloudFro?=
=?UTF-8?q?nt=20=E5=AE=9E=E7=8E=B0=20Web=20=E6=9C=8D=E5=8A=A1=E5=9C=A8?=
=?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=8C=BA=E5=9F=9F=E7=9A=84=E8=AE=BF=E9=97=AE?=
=?UTF-8?q?=E9=99=90=E5=88=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
content/knowledge-center/cloudfrout.md | 172 +++++++++++++++++++++++++
1 file changed, 172 insertions(+)
create mode 100644 content/knowledge-center/cloudfrout.md
diff --git a/content/knowledge-center/cloudfrout.md b/content/knowledge-center/cloudfrout.md
new file mode 100644
index 0000000..99ca3fc
--- /dev/null
+++ b/content/knowledge-center/cloudfrout.md
@@ -0,0 +1,172 @@
+---
+slug: cloudfrout
+title: 使用 CloudFront 实现 Web 服务在不同区域的访问限制
+description: 本文介绍了如何使用 AWS CloudFront 配合 Lambda@Edge 功能,根据客户端的地理位置信息(国家/地区)实现对 Web 服务的访问限制或重定向,从而优化用户体验并提高安全性。
+keywords: "云计算, cdn, CloudFront"
+category: Amazon Web Services
+order: 4
+---
+
+
+
+# 使用 CloudFront 实现 Web 服务在不同区域的访问限制
+
+## 1. 准备工作
+
+在对接之前,确保以下条件满足:
+
+- **Web 服务器可访问**:Web 服务器必须能够通过公网访问(HTTP/HTTPS)。如果 Web 服务器位于私有网络(如 VPC),需要配置公网访问(如通过 NAT 网关或负载均衡器)。
+
+---
+
+## 2. 创建 CloudFront 分发
+
+1. 进入 AWS 控制台,选择 **CloudFront** 服务。
+2. 点击 **Create Distribution**(创建分发)。
+3. 配置源站(Origin)为您的 Web 服务器地址。
+4. 根据需要选择是否启用 **WAF**(Web Application Firewall),注意 WAF 会产生额外费用。
+
+
+
+
+
+
+---
+
+## 3. 配置 CloudFront 的源站请求策略
+
+### 1. 创建策略
+1. 在新建的页面,输入策略名称(如 `client-ip-and-country`),并填写备注。
+
+### 2. 配置选项
+配置以下选项:
+
+#### a. Headers
+选择 **All viewer headers and the following CloudFront headers**,表示转发所有客户端请求的 Header,并额外追加 CloudFront 的 Header,包括:
+- `CloudFront-Viewer-Address`:客户端 IP 地址。
+- `CloudFront-Viewer-Country`:客户端国家代码。
+- `CloudFront-Viewer-Country-Name`:客户端国家名称。
+- `CloudFront-Viewer-Country-Region`:客户端区域代码。
+- `CloudFront-Viewer-Country-Region-Name`:客户端区域名称。
+
+#### b. Query strings
+选择 **All**,表示转发所有请求字符串。
+
+#### c. Cookies
+选择 **All**,表示转发所有请求的 Cookie。
+
+
+
+
+
+## 4. 修改现有发布点的策略
+
+### 1. 进入 CloudFront 分发界面
+进入 CloudFront 分发界面,点击 **Behaviors(行为)** 标签页。
+
+### 2. 找到并编辑源站
+找到目标源站并选中,点击 **Edit(编辑)**。
+
+### 3. 确保配置生效
+确保配置的源站请求策略已生效。
+
+
+
+
+## 5. 创建 Lambda 函数
+
+### 1. 进入 AWS Lambda 控制台
+进入 AWS Lambda 控制台,点击 **Create Function(创建函数)**。
+
+
+### 2. 使用蓝图创建
+选择 **Use a blueprint(使用蓝图)**,并在蓝图列表中选择 **getting-started-with-lambda-http**。
+
+
+
+### 3. 创建函数时,取消添加触发器
+在创建函数的过程中,选择 **Cancel(取消)** 添加触发器。
+
+
+
+
+### 4. 输入以下代码
+在 Lambda 函数的代码编辑器中,输入以下代码:
+
+
+```js
+'use strict';
+
+exports.handler = (event, context, callback) => {
+ const request = event.Records[0].cf.request;
+ const headers = request.headers;
+
+ let url = 'https://example.com/';
+
+ if (headers['cloudfront-viewer-country']) {
+ const countryCode = headers['cloudfront-viewer-country'][0].value;
+
+ if (countryCode === 'US') {
+ url = 'https://www.google.com/'; // 替换为美国用户的 Web 服务器地址
+ } else if (countryCode === 'KR') {
+ url = 'https://www.bilibili.com/'; // 替换为韩国用户的 Web 服务器地址
+ } else if (countryCode === 'SG') {
+ url = 'https://www.AWS.com/'; // 替换为新加坡用户的 Web 服务器地址
+ }
+ }
+
+ const response = {
+ status: '302',
+ statusDescription: 'Found',
+ headers: {
+ location: [{
+ key: 'Location',
+ value: url,
+ }],
+ },
+ };
+
+ callback(null, response);
+};
+```
+选择保存
+## 6. 配置 IAM 角色
+
+### 1. 进入 IAM 控制台
+进入 IAM 控制台,找到 Lambda 函数的执行角色。
+
+### 2. 添加信任关系策略
+在 **信任关系** 中,添加以下策略:
+
+```yaml
+{
+ "Version": "2012-10-17",
+ "Statement": [
+ {
+ "Effect": "Allow",
+ "Principal": {
+ "Service": "lambda.amazonaws.com"
+ },
+ "Action": "sts:AssumeRole"
+ }
+ ]
+}
+```
+## 7. 添加触发器
+
+### 1. 在 Lambda 函数页面,点击 **Add Trigger(添加触发器)**
+
+
+### 2. 选择 **CloudFront** 作为触发器
+
+
+### 3. 配置触发器并部署
+
+
+## 8. 测试与验证
+
+### 1. 进入 CloudFront 分发界面,获取分配的域名
+
+
+### 2. 使用不同区域的客户端访问该域名,验证是否根据国家/地区重定向到不同的 Web 服务器
+
\ No newline at end of file