Nice programing

Amazon S3 리디렉션 및 Cloudfront

nicepro 2020. 12. 7. 20:40
반응형

Amazon S3 리디렉션 및 Cloudfront


http://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html 에서 참조하는 객체를 사용하여 S3에서 301 리디렉션을 설정하려고합니다 . 나는 몇 가지 문제를 겪었고 내가 뭘 잘못하고 있는지 알아낼 수없는 것 같습니다.

내가 얻는 것은 '웹 사이트 리디렉션 위치'메타 데이터 값이 설정되지 않은 것처럼 빈 페이지 (0 바이트 파일)입니다.

내가 도대체 ​​뭘 잘못하고있는 겁니까?

또한 이것은 AWS CloudFront에서 작동합니까?


내 S3 콘솔 설정 S3 콘솔 설정

참고할 몇 가지 사항 :

정적 사이트를 호스팅하기위한 설정이 있습니다. 내 자신의 인증서가 업로드되고 cloudfront 배포에 설정된 ssl / https를 사용하고 있습니다. 리디렉션 개체를 제외하고 모든 페이지가 작동하는 것 같습니다. 라우팅 규칙 설정을 시도했지만 Cloudfront에서 작동하지 않는 것 같습니다.

I'm trying to access the redirects both through the cloudfront url and the s3 url (https://s3.amazonaws.com/{bucket}/users/sign_in)


For web site-like functionality in S3, such as redirects, html error messages, and index documents, you can't use the REST endpoint (${bucket_name}.s3.amazonaws.com or ${bucket_name}.s3.${region}.amazonaws.com) since these features are only provided by the web site endpoints (${bucket_name}.s3-website.${region}.amazonaws.com).

http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html

To make the behavior available in Cloudfront, you need to configure Cloudfront to use this endpoint, as well, not the REST one offered via autocomplete in the console.

Enter the Amazon S3 static website hosting endpoint for your bucket. This value appears in the Amazon S3 console, on the Properties page under Static Website Hosting.

When you specify the bucket name in this format, you can use Amazon S3 redirects and Amazon S3 custom error documents.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DownloadDistS3AndCustomOrigins.html

Note that the web endpoints do not support HTTPS, but you can configure Cloudfront to fetch from the bucket with HTTP even though the client connection is HTTPS.


Helpful tip: when troubleshooting and testing changes with CloudFront, users are often confused by the apparent "latency" between when you make changes and when CloudFront starts exhibiting the new behavior. In the case of error pages, CloudFront has a default 5 minute Error Caching Minimum TTL that prevents it from re-sending requests for failed pages to the origin, and this is a separate timer from the minimum/default/maximum TTL set in Cache Behavior. Particularly when testing, you may want to disable these timers and force a retry with each subsequent request of pages that returned errors, using the steps I provided in the answer to a question about apparent Amazon CloudFront Latency.


I had the same problem with redirects failing and zero-length files getting downloaded, but wasn't using Cloudfront.

The root cause in my case was my DNS CNAME record pointed to the REST endpoint (static.righto.com.s3-us-west-2.amazonaws.com) instead of the website endpoint (static.righto.com.s3-website-us-west-2.amazonaws.com). Updating the CNAME record fixed my redirects.

Another symptom of this problem was missing pages displayed an XML error page rather than an HTML error page.

This document explains the difference between website endpoints and REST endpoints.


If you come here to find solution without using CloudFront, here it is:

You can use s3cmd (in bash script, of course) and its parameters:

  • --add-header with x-amz-website-redirect-location

For example, this script solves problem for page /about/:

s3cmd \
--acl-public \
--add-header "x-amz-website-redirect-location: /about/" \
--no-preserve \
put "./path/to/any/small/file/at/your/local/drive/index.html" "s3://domain.com/about"

이 스크립트는 후행 슬래시없이 파일 이름으로 일부 기존 파일을 복사 합니다 . 브라우저가 즉시 리디렉션되기 때문에 파일에 무엇이 포함되어 있는지는 중요하지 않습니다.

당신이 (더 중요 할 때 웹 크롤러가 ) 열려있는 페이지로하려고 /about하면 얻을 301로 리디렉션 /about/. 이를 통해 Google, yandex 및 기타 검색 엔진의 캐시올바른 페이지 를 유지할 수 있습니다 .

여러 페이지에 대해이 코드를 반복 할 수 있습니다. 또는 폴더 검색 기능을 추가하고 각 폴더를 호출 할 수 있습니다. 아마 당신은 여기에 그것을 보여 주면 너무 친절 할 것입니다. :)

참고 URL : https://stackoverflow.com/questions/22740084/amazon-s3-redirect-and-cloudfront

반응형