在 ec2 实例上使用 Cloudfront 获取 405 错误代码

Getting 405 error code when using Cloudfront to origin on ec2 instance

我创建了一个连接到 2 个来源的云端分布: 1.我上传的s3静态网站。 2. 带有 nginx docker 的 ec2 实例,配置如下:

location /api {
        proxy_pass http://my_app:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  client_max_body_size 1000M;
  server_tokens off;

我配置了2个行为 具有默认 (*) 路径模式的 s3 允许所有 HTTP 方法使用原始缓存 headers。转发所有缓存和字符串。

ec2 路径模式 api 允许所有 HTTP 方法使用原始缓存 headers。转发所有缓存和字符串。

我得到 405

<html>
<head><title>405 Method Not Allowed</title></head>
<body>
<h1>405 Method Not Allowed</h1>
<ul>
<li>Code: MethodNotAllowed</li>
<li>Message: The specified method is not allowed against this resource.</li>
<li>Method: POST</li>
<li>ResourceType: OBJECT</li>
<li>RequestId: 871316565A038A65</li>
<li>HostId: BHbdP09UWX2xCvW6qqpOQYaGwy4lvgrVVYoSCaLrSBZ2TALZT5kk9Po7k6Akq2FYZ6NojFyINBk=</li>
</ul>
<hr/>
</body>
</html>

响应来自 S3,您可以在响应中看到 RequestId 和 HostId。 S3 支持 POST http 请求,但你需要确保它有 Form-Data。具有 POST 请求的 S3 仅接受表单数据。 https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html

如果您不能使用 Form-data,请使用 PUT 请求到 CloudFront。