AWS CloudFront에서 default root object 모든 Path 적용 이슈 우회 방법



- 작업 개요

Cloud Front의 경우 Default Root Object 설정이 최상위 Directory 에서 밖에 적용이 되지 않는다.

S3에서 웹사이트 호스팅(Static website hosting)을 할때 S3에서 [Index document] 를 index.html로 설정한다면 

아래 표와 같기 Redirect를 해준다.

접속 URL 

Redirect URL 

 www.example.com/

 www.example.com/index.html

 www.example.com/test/ 

 www.example.com/test/index.html


그러나 Cloud Front 에서는 아무리  Default Root Object를 설정해도 최상위 Path를 제외하고는 오류가 발생한다.

접속 URL 

Redirect URL 

 www.example.com/

 www.example.com/index.html

 www.example.com/test/ 

ERROR(403)


따라서 Cloud Front를 적용 할 떄 최상위 path를 제외하고는 url을 전체 다 입력 해줘야한다.

해당 이슈에대한 우회 방법으로 Path와 같은 파일 명으로 html 파일을 생성고,

그 파일 S3에 업로드 할때 Header에  Content-Type을 text/html로 설정하면 해당 파일을 html로인식해서 

www.example.co.kr/test/ 로 입력해도 CloudFont 서비스가 가능해진다.



- 작업 절차

웹 서비스를 할 S3에 html 파일을 업로드하고, html 파일 명을 확장자를 제외한 Path 명으로 변경한다.

그런 다음 html 파일의 Meta data에서 Content-Type을 text/html로 변경한다. 

그리고 Cloud Front의 캐시를 초기화 시킨다.



1. S3에서 파일을 선택한다음 [Properties]-[Metadata]에서 

[Content-Type]을 [text/html]로 변경해준다.


2. 그런 다음 파일 명에서 확장자를 지운다.

3. CloudFont 에서 Invalidations 탭에 아래와 같이 edge location에 cache를 초기화 시킨다.

(이미 edge Location에 올라가있는 파일은 Metadata 값이 TTL 시간이 지나더라도 초기화 되지 않으므로 꼭 이 작업을 해주어야한다.)



해당 작업을 완료하면 CloudFont와 S3를 사용해 https 서비스를 사용하면서 모든 Root에 index 파일을 설정한것 처럼

millionairedeveloper.example.co.kr/test/

test 가 html이기 때문에 서비스가 가능해진다.




그럼 오늘도 행복한 하루 되세요.

'AWS > S3' 카테고리의 다른 글

AWS S3를 EC2 인스턴스에 s3fs를 이용한 mount 예제  (2) 2019.01.28

AWS S3를 EC2 인스턴스에 Mount 예제


작업 개요

- S3를 서버에서 일반 스트로리지 또는 NAS를 mount 한 형태처럼 사용하기 위해 s3fs 오픈 소스를 사용해 서버에 mount를 하는 작업이다.

작업 환경 

서비스 : EC2

AMI : Amazon Linux AMI 2018.03.0


작업 내용 

- s3fs를 설치하고 s3 버킷을 서버에 mount 한다.



작업 절차 


1. yum update

  sudo yum  -y update all


2. 필요한 페키지 설치

  sudo yum -y install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel


3. /usr/src 이동

  cd /usr/src


4. s3fs-fuse 프로젝트 다운로드

  sudo git clone https://github.com/s3fs-fuse/s3fs-fuse.git


5. 프로젝트 소유자 변경

  sudo chown -R ec2-user:ec2-user s3fs-fuse


6. s3fs-fuse 로이동

  cd s3fs-fuse


7. autogen.sh 실행

  ./autogen.sh


8. configure 실행

  ./configure --prefix=/usr --with-openssl


9. make 실행

  make


10. make install 실행

  sudo make install


11. s3fs 위치 확인

  which s3fs


12. s3 접근 가능한 IAM 설정 파일 만들기

  sudo vi /etc/passwd-s3fs

  - 파일 내용

  Access Key ID:Secret Access Key

  ex ) AXXXXXXXXXXXXXXXXXX:ZhiZXXXXXXXXXXXXXXXXXXXXX


13. 파일 작업 권한 및 소유권 설정

  sudo chmod 600 /etc/passwd-s3fs

  sudo chown  ec2-user:root /etc/passwd-s3fs


14. mount 할 디렉토리 생성

  sudo mkdir /mys3bucket1


15. 버킷 mount

    sudo s3fs [버킷 명] [마운트 경로]

      -o use_cache=/tmp

      -o allow_other

      -o uid=[사용자 ID]

      -o gid=[사용자 그룹 ID]

      -o multireq_max=20

      -o use_path_request_style

      -o url=https://s3-[리전 id].amazonaws.com


    ex) sudo s3fs s3bucketname /mys3bucket1 -o use_cache=/tmp -o allow_other -o uid=500 -o gid=500 -o multireq_max=5 -o use_path_request_style -o url=https://s3-ap-northeast-2.amazonaws.com


    ** 참고

      -EC2 user ID 확인 :  id -u [user name]

      -EC2 user 그룹 ID 확인 :  id -g [user name]

- umount 방법 

- pkill -9 -t s3fs

그럼 오늘도 행복한 하루 되세요.


+ Recent posts