在 AWS 中設置 HTTP 和 HTTPS 端口開放,主要是透過 Security Groups(安全群組)來配置。以下是詳細步驟:
透過 AWS 控制台設置
1. 進入 EC2 控制台
- 登入 AWS 管理控制台
- 選擇 EC2 服務
- 在左側選單點選「Security Groups」
2. 選擇或建立安全群組
- 選擇你要修改的安全群組
- 或點選「Create security group」建立新的
3. 設置入站規則(Inbound Rules)
HTTP (端口 80):
- 點選「Edit inbound rules」
- 點選「Add rule」
- Type:選擇「HTTP」
- Protocol:TCP
- Port Range:80
- Source:選擇 0.0.0.0/0(允許所有 IP)或指定特定 IP 範圍
HTTPS (端口 443):
- 再次點選「Add rule」
- Type:選擇「HTTPS」
- Protocol:TCP
- Port Range:443
- Source:選擇 0.0.0.0/0 或指定 IP 範圍
4. 儲存設定
- 點選「Save rules」完成設置
透過 AWS CLI 設置
# 開放 HTTP 端口 80
aws ec2 authorize-security-group-ingress \
--group-id sg-xxxxxxxxx \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0
# 開放 HTTPS 端口 443
aws ec2 authorize-security-group-ingress \
--group-id sg-xxxxxxxxx \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0
注意事項
安全考量:
- 0.0.0.0/0 表示允許所有 IP 存取,適用於公開網站
- 如果是內部系統,建議限制特定 IP 範圍
- 定期檢查和更新安全群組規則
應用到實例:
- 確保你的 EC2 實例已關聯到此安全群組
- 可在實例詳細資訊中的「Security」標籤檢查
防火牆設定:
- AWS 安全群組只是第一層防護
- 記得檢查實例內部的防火牆設定(如 iptables、ufw)
- 確保應用程式有在相應端口上監聽
這樣設置後,你的 EC2 實例就能接收來自網際網路的 HTTP 和 HTTPS 流量了。