S3 上的 CloudFront 静态托管,在自定义源中具有负载平衡 API

CloudFront Static Hosting on S3 with a Load Balanced API in a Custom Origin

我正在使用 CloudFront,但我 运行 遇到了自定义来源的问题。我在 S3 上有我的前端,它们都是静态的。 CloudFront 正在为这些服务提供良好的服务。当我设置新行为以路由到负载均衡器后面的后端 API 时,CloudFront 实际上并未路由流量。 Path Pattern 和 Origin 定义正确 - 只是没有路由。

我可以点击我的前端:

https://somerandomstring.cloudfront.net/apps/myapp/resources/index.html

登录时,它会尝试 POST 到 /myapi/rest/Login?<some_query_string>

我已经在 CloudFront 中配置了两个源。

+---------------------------+------------+-------------+---------+
| Origin                    |            | Origin | Protocol     |
| Name/Path                 | ID         | Type   | Policy       |
+===========================+============+========+==============+
| mybucket.s3.amazonaws.com | S3-origin  | S3     | None         |
| myelb.amazonaws.com       | ELB-origin | Custom | Match Viewer |
+---------------------------+------------+--------+--------------+

并且我已经配置了我的行为。

+---------------+------------+---------------------+-------------+
|               |            | Viewer              | Query       |
|      Pattern  |   Origin   | Protocol Policy     | Strings Fwd |
+===============+============+=====================+=============+
| /myapi/rest/* | ELB-origin | Redir HTTP to HTTPS | Yes         |
| Default       | S3-origin  | Redir HTTP to HTTPS | Yes         |
+---------------+------------+---------------------+-------------+

Side note: I spent entirely too much time formatting those tables...

当静态前端尝试 POST 到 /myapi/rest/Login?<some_query_string> 时,我收到 502:

+-----------------------------------------------------------+--------+
|                           Name                            | Status |
+===========================================================+========+
| https://cname.cloudfront.net/myapi/rest/Login?<query_str> |    502 |
+-----------------------------------------------------------+--------+

一个curl -ireturns以下:

HTTP/2 502 
content-type: text/html
content-length: 587
server: CloudFront
date: Fri, 15 Dec 2017 21:38:13 GMT
x-cache: Error from cloudfront
via: 1.1 861354a025845bd3b87dc25ce80431df.cloudfront.net (CloudFront)
x-amz-cf-id: XKj4fuF6qGAQd-JBIftvUhoAh5SPnzqBOT2HwUQiMIruqiIJhZ5fuw==

<!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: vEQTkS9fmjfQqlWA4ICS62DOLTHps-6wjeVWKvBz0XTNex39Um_wjQ==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY>

没有任何东西在后端访问服务器,CloudFront 日志中也没有任何帮助。但是,我可以 POST 直接连接到我的负载均衡器 https://myelb.amazonaws.com/myapi/rest/Login?<some_query_string> 就好了。在这一点上,我完全被难住了。如果可能的话,我真的很想避免使用像 Nginx 这样的东西。

关于我可能做错了什么的想法?

事实证明这是一个 SSL 证书问题。我在负载均衡器上附加了错误的证书,它抛出了 502。

对于可能 运行 涉及此问题的任何其他人,请仔细检查您的证书。还要确保您的密码兼容。