在AWS静态网站上启用SSL,需要进行/index处理。下面是一个解决方法的代码示例:
import json
def lambda_handler(event, context):
request = event['Records'][0]['cf']['request']
headers = request['headers']
# 检查是否请求的是根路径
if request['uri'] == '/':
# 检查是否已经使用https访问
if headers['cloudfront-forwarded-proto'][0]['value'] != 'https':
# 构建重定向URL
redirect_url = 'https://' + headers['host'][0]['value'] + '/index.html'
# 返回重定向响应
return {
'status': '302',
'statusDescription': 'Found',
'headers': {
'location': [{
'key': 'Location',
'value': redirect_url,
}],
},
}
# 如果请求不是根路径或已经使用https访问,则继续正常处理
return request
创建一个Lambda@Edge函数来触发Lambda函数
配置CloudFront分配
通过这些步骤,您可以在AWS静态网站上启用SSL,并确保/index请求正常工作。
上一篇:AWS静态网站访问错误的S3目录