编写使用 CloudFront 实现 Web 服务在不同区域的访问限制
This commit is contained in:
parent
711f22953c
commit
cfbaca8d6c
172
content/knowledge-center/cloudfrout.md
Normal file
172
content/knowledge-center/cloudfrout.md
Normal file
@ -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 会产生额外费用。
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image1.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image2.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image3.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image4.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image5.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 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。
|
||||||
|
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image6.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image7.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image8.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
## 4. 修改现有发布点的策略
|
||||||
|
|
||||||
|
### 1. 进入 CloudFront 分发界面
|
||||||
|
进入 CloudFront 分发界面,点击 **Behaviors(行为)** 标签页。
|
||||||
|
|
||||||
|
### 2. 找到并编辑源站
|
||||||
|
找到目标源站并选中,点击 **Edit(编辑)**。
|
||||||
|
|
||||||
|
### 3. 确保配置生效
|
||||||
|
确保配置的源站请求策略已生效。
|
||||||
|
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image9.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image10.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
## 5. 创建 Lambda 函数
|
||||||
|
|
||||||
|
### 1. 进入 AWS Lambda 控制台
|
||||||
|
进入 AWS Lambda 控制台,点击 **Create Function(创建函数)**。
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image11.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
### 2. 使用蓝图创建
|
||||||
|
选择 **Use a blueprint(使用蓝图)**,并在蓝图列表中选择 **getting-started-with-lambda-http**。
|
||||||
|
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image12.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
### 3. 创建函数时,取消添加触发器
|
||||||
|
在创建函数的过程中,选择 **Cancel(取消)** 添加触发器。
|
||||||
|
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image13.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image14.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
### 4. 输入以下代码
|
||||||
|
在 Lambda 函数的代码编辑器中,输入以下代码:
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image15.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
```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. 添加信任关系策略
|
||||||
|
在 **信任关系** 中,添加以下策略:
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image16.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
```yaml
|
||||||
|
{
|
||||||
|
"Version": "2012-10-17",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Principal": {
|
||||||
|
"Service": "lambda.amazonaws.com"
|
||||||
|
},
|
||||||
|
"Action": "sts:AssumeRole"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
## 7. 添加触发器
|
||||||
|
|
||||||
|
### 1. 在 Lambda 函数页面,点击 **Add Trigger(添加触发器)**
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image17.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
### 2. 选择 **CloudFront** 作为触发器
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image18.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
### 3. 配置触发器并部署
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image19.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
## 8. 测试与验证
|
||||||
|
|
||||||
|
### 1. 进入 CloudFront 分发界面,获取分配的域名
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image20.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
|
|
||||||
|
### 2. 使用不同区域的客户端访问该域名,验证是否根据国家/地区重定向到不同的 Web 服务器
|
||||||
|
<img src="http://52.220.40.88:81/CloudFront/image21.jpg" alt="示例图片" style="width:100%; height:auto;">
|
||||||
Loading…
x
Reference in New Issue
Block a user