CloudFront使用路径前缀重定向所有请求

时间:2016-06-06 17:08:54

标签: amazon-s3 amazon-cloudfront amazon-elb

我有一个静态网站,它向API服务器发出请求。我使用S3托管这些静态页面,我想使用CloudFront将api调用重定向到api服务器。 Api调用可以通过api:

的路径前缀来区分
domain.com/index.html         s3/index.html
domain.com/js/index.js        s3/js/index.html
domain.com/api/request        api_server/api/request
domain.com/api/other/request  api_server/api/other/request

我目前设置了两个来源 - 我的S3存储桶的一个S3源(工作正常)和我的api服务器的另一个自定义源(无效)。

自定义原点设置如下:

Origin Domain Name: api_elb
Origin path: /

,行为设置如下:

Precedence: 0
Path pattern: /api/*
Allowed HTTP Methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
Forward Headers: all
Forward Query Strings: yes

以下是完整回复:

> GET /api/logout HTTP/1.1
> Host: a0000aaaaaaaaa.cloudfront.net
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
< Content-Type: text/html
< Content-Length: 587
< Connection: keep-alive
< Server: CloudFront
< Date: Mon, 06 Jun 2016 19:37:45 GMT
< X-Cache: Error from cloudfront
< Via: 1.1 86f2bbfea57d6217ce2d279b84ca3743.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: A18vpnPPr0cWn5VuVirvvKc-wocC9OcXjNDHZm-PyvD3ONGcPmqmmg==
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
CloudFront wasn't able to connect to the origin.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: -u68y2nD9tuaxL_2MjSk3udA1r64v-K3xoGitKFYvd9Z48gB8ntnwg==
</PRE>
<ADDRESS>
</ADDRESS>

此配置有什么问题?如何让CloudFront将这些请求转发给API?它当前正在为所有API请求返回502(错误网关)响应。

1 个答案:

答案 0 :(得分:0)

这看起来像是在这里回答:

https://stackoverflow.com/a/23599450/2542922

我将api_server.company.com添加到备用域名。