AWS CloudFront误解了路由规则,并将资源重定向回S3存储桶对象URL [英] AWS CloudFront misinterprets routing rule and redirects resource back to S3 bucket object URL

查看:228
本文介绍了AWS CloudFront误解了路由规则,并将资源重定向回S3存储桶对象URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AWS S3存储桶;我们称之为 example.com 。我已将存储桶配置为用于静态网站托管;例如,存储桶静态网站URL可能是 http://example.com.s3-website-us-west-1.amazonaws.com 。我也有一个带有AWS托管证书的CloudFront发行版,因此,例如,当我访问 https://example.com/ 时,它从S3存储桶中提供内容 http://example.com.s3-website-us-west-1.amazonaws.com/ 。一切都像梦一样。

I have an AWS S3 bucket; let's call it example.com. I have the bucket configured for static web site hosting; the bucket static site URL for example might be http://example.com.s3-website-us-west-1.amazonaws.com. I also have a CloudFront distribution with an AWS-managed certificate, so that when I access https://example.com/ for example it serves content out of the S3 bucket http://example.com.s3-website-us-west-1.amazonaws.com/. That all works like a dream.

在我的网站上,我有一个文件 https://example.com/foo.html 已重命名为 https://example.com/bar.html 。网络上的其他某些页面可能会链接到 foo.html ,因此在我的S3存储桶配置中,我为 foo.html 设置了重定向code>对象,例如:

On my site I have a file https://example.com/foo.html that has been renamed to https://example.com/bar.html. Some other pages on the web may link to foo.html, so in my S3 bucket configuration I set up a redirect for the foo.html object, like this:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>foo.html</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>bar.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
</RoutingRules>

当我尝试浏览到 http://example.com.s3时-website-us-west-1.amazonaws.com/foo.html ,它将HTTP 301 永久重定向发送到 bar.html ,所以我最后进入 http://example.com.s3-website-us-west-1.amazonaws.com/bar.html ,就像我期望的那样。

When I try to browse to http://example.com.s3-website-us-west-1.amazonaws.com/foo.html, it sends an HTTP 301 permanent redirect to bar.html, so I wind up at http://example.com.s3-website-us-west-1.amazonaws.com/bar.html, just like I expect.

因此,现在如上所述,我将S3存储桶部署到CloudFront发行版中。在CloudFront状态显示为已部署后,我浏览至 https://example.com/foo.html ,希望将其重定向至 https: //example.com/bar.html 。不好了;我被定向到 http://example.com.s3-website-us-west-1.amazonaws.com/foo.html insetad!

So now I deploy the S3 bucket to a CloudFront distribution as explained above. After the CloudFront status shows "Deployed", I browse to https://example.com/foo.html, expecting to be redirected to https://example.com/bar.html. Oh, no; I am directed to http://example.com.s3-website-us-west-1.amazonaws.com/foo.html insetad!

为什么CloudFront发行版将我重定向回原义的S3存储桶URL?为什么对象不能重定向到正确存储到CloudFront的同一存储桶中的相对对象?

Why is the CloudFront distribution redirecting me back to the literal S3 bucket URL? Why doesn't the object redirect to a relative object in the same bucket being correctly deployed to CloudFront?

请注意,我知道另一个问题 AWS Cloudfront重定向到S3存储桶,但是似乎不是这个问题的重复问题,因为另一个问题是指整个CloudFront站点暂时重定向到整个S3存储桶,而不是单个S3对象重定向无法部署到CloudFront的失败。

Note that I am aware of the other question AWS Cloudfront redirecting to S3 bucket, but that does not appear to be a duplicate question of this one, as the other question is referring to the entire CloudFront site temporarily redirecting to the entire S3 bucket, not the failure of an individual S3 object redirect to be deployed to CloudFront.

推荐答案

首先,CloudFront不遵循从源头进行重定向,而是从S3接收响应并将其按原样提供给客户端。
S3发送此响应是因为在S3重定向规则中,您还需要指定HOST值。

First of all, CloudFront doesn't follow redirection from origin and it received the response from S3 and it served it to the client as it is. S3 is sending this response because in S3 redirect rules, you also need to specify the HOST value.

例如:

<HostName>...</HostName>

您可以将主机名定义为example.com或 https://example.com ,将在响应中返回的Location标头中使用的主机名。

You can define hostname as the name example.com ot https://example.com, The hostname to be used in the Location header that is returned in the response.

https://docs.aws .amazon.com / AmazonS3 / latest / dev / how-to-page-redirect.html

注意:在进行更改之前,请记住清除缓存然后再次测试,cloudfront还会缓存3xx响应。

Note: Remember to clear the cache before you make changes and test again, cloudfront also caches 3xx response.

这篇关于AWS CloudFront误解了路由规则,并将资源重定向回S3存储桶对象URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆