BREAKING:

Cách bật HTTPS cho API trên EC2 AWS

1. Kiểm tra port & Security Group AWS

  • Vào AWS EC2 ConsoleSecurity Groups → mở port 3000 cho cả HTTP (TCP)HTTPS (TCP).
    • Port 80 (HTTP)
    • Port 443 (HTTPS)
    • Nếu muốn truy cập trực tiếp :3000 qua HTTPS thì cũng phải mở 3000. Nhưng thông thường ta chỉ để HTTPS chạy ở 443.

👉 Khuyến nghị: không expose 3000 ra ngoài. Thay vào đó để Node chạy nội bộ port 3000, rồi dùng Nginx reverse proxy ở port 80/443.


2. Cài Nginx (reverse proxy)

Trên EC2 (Ubuntu ví dụ):

sudo apt update
sudo apt install nginx -y

Cấu hình virtual host /etc/nginx/sites-available/api.titach.com:

server {
    listen 80;
    server_name api.titach.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Kích hoạt:

sudo ln -s /etc/nginx/sites-available/api.titach.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

3. Cài SSL miễn phí bằng Certbot

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d api.titach.com

Certbot sẽ:

  • Tự động xin chứng chỉ từ Let’s Encrypt
  • Cập nhật cấu hình Nginx thành HTTPS (port 443)

Sau khi hoàn tất, bạn sẽ truy cập được:

https://api.titach.com/api

(ngay cả khi backend chạy ở 3000, Nginx proxy sẽ đảm bảo domain ra 443 chuẩn).

Post A Comment

Your email address will not be published. Required fields are marked *

Leave a Reply