์ต์ข ์ํคํ ์ฒ:
[User]
โ
CloudFront (Admin - HTTPS)
โ
S3 (React SPA)
[User]
โ
CloudFront (API - HTTPS)
โ
EC2 (NestJS :3000)
๐ ํต์ฌ ๋ชฉํ
S3 โ Bucket โ Properties โ Static website hosting โ Enable
์ค์ :
Index document: index.html
Error document: index.html (SPA ํ์)
๐ React Router ์ฌ์ฉ ์ ํ์
S3 โ Permissions โ Block public access โ ์ ๋ถ ํด์
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::๋ฒํท๋ช
/*"
}
]
}
http://๋ฒํท๋ช
.s3-website-๋ฆฌ์ .amazonaws.com
Origin Domain: S3 website endpoint (์ค์)
์:
chain-wallet-host.s3-website-ap-northeast-1.amazonaws.com
Viewer Protocol Policy: โ Redirect HTTP โ HTTPS
Default Root Object: โ index.html
CloudFront โ Error Pages
403 โ /index.html โ 200
404 โ /index.html โ 200
๐ ์ด๊ฑฐ ์ ํ๋ฉด /dashboard ์ง์ ์ ์ ์ 404
CloudFront (HTTPS) โ EC2 API (HTTP)
๐ ๋ธ๋ผ์ฐ์ ์ฐจ๋จ
CloudFront (API) โ Origin: EC2 (IP:3000)
Origin:
โ Custom Origin
โ HTTP only
โ Port 3000
Allowed Methods:
GET / POST / PUT / PATCH / DELETE / OPTIONS
Cache:
Disabled
Origin Request Policy:
AllViewer
Allowed Methods:
GET / POST / PUT / PATCH / DELETE / OPTIONS
Cache:
Disabled
Origin Request Policy:
AllViewer
๐ ์ด์ API๋ HTTPS
https://xxxxx.cloudfront.net
VITE_API_BASE_URL=https://CloudFront_API_URL
.env ๋ณ๊ฒฝ ํ ๋ฐ๋์ build + ์ฌ๋ฐฐํฌ
ํ์ฉ origin โ ์ค์ ์์ฒญ origin
app.enableCors({
origin: true,
credentials: true,
});
app.enableCors({
origin: [
'https://admin-cloudfront-url',
],
credentials: true,
});
index.html ์บ์ฑ/*
aws s3 cp dist/index.html s3://๋ฒํท/index.html \
--cache-control "no-cache, no-store, must-revalidate"
aws s3 cp dist/assets s3://๋ฒํท/assets \
--recursive \
--cache-control "public, max-age=31536000, immutable"
<meta http-equiv="Cache-Control">
๐ ํจ๊ณผ ์์ (CloudFront์๋ ์ํฅ ์์)
set -e
BUCKET=๋ฒํท๋ช
DIST_ID=CloudFront_ID
aws s3 cp dist/assets s3://$BUCKET/assets \
--recursive \
--cache-control "public, max-age=31536000, immutable"
aws s3 cp dist/index.html s3://$BUCKET/index.html \
--cache-control "no-cache, no-store, must-revalidate"
aws cloudfront create-invalidation \
--distribution-id $DIST_ID \
--paths "/*"
"deploy:admin": "pnpm build:admin && pnpm --filter @cws/admin deploy"
"deploy": "sh deploy.sh"
์ด ๊ตฌ์กฐ๋ ์ค์ ๋ธ๋ก์ฒด์ธ ์ ์ถ๊ธ ์์คํ ์ Admin UI ๋ฐฐํฌ ๊ตฌ์กฐ์ ๋์ผํ๋ค.
์ด ํ๋ฆ์ ์์คํ ์ํคํ ์ฒ์์๋ ๋์ผํ๊ฒ ๋ถ๋ฆฌ๋์ด ์ค๊ณ๋๋ค
| ๋ฌธ์ | ์์ธ | ํด๊ฒฐ |
|---|---|---|
| Mixed Content | HTTP API | CloudFront(API) |
| CORS | origin mismatch | origin ์ค์ |
| ์ฝ๋ ๋ฏธ๋ฐ์ | index.html ์บ์ | no-cache + invalidation |
origin: CloudFront ๋๋ฉ์ธ๋ง ํ์ฉ
/ โ S3
/api โ EC2
๐ CORS ์ ๊ฑฐ ๊ฐ๋ฅ
admin.xxx.com
api.xxx.com
S3 + CloudFront + CloudFront(API) ๊ตฌ์กฐ๋ก
HTTPS ๊ธฐ๋ฐ SPA + API ํต์ ํ๊ฒฝ ์์ฑ
ํ๋ก ํธ ๋ฐฐํฌ ๋ฌธ์ ์ 90%๋ ์บ์๋ค