Hướng dẫn cấu hình SSL cho domain trong HAProxy

Views: 13 19/04/2026 07:57
Hướng dẫn cấu hình SSL cho domain trong HAProxy

HAProxy là một load balancer và reverse proxy mạnh mẽ, thường được dùng để xử lý traffic lớn và phân phối request đến backend server.

Việc cấu hình SSL giúp website của bạn hoạt động qua HTTPS, đảm bảo bảo mật dữ liệu và tăng độ tin cậy.

Chuẩn bị trước khi cấu hình

Bạn cần có:

  • File Private Key (.key)

  • File Certificate (.crt)

  • File CA Bundle / Intermediate (nếu có)

Nếu bạn dùng SSL từ các nhà cung cấp như Let's Encrypt, bạn sẽ có sẵn các file này.

Tạo file .pem cho HAProxy

HAProxy yêu cầu tất cả SSL phải nằm trong một file duy nhất (.pem).

Thứ tự bắt buộc:

Private Key → Certificate → CA Bundle

Lệnh gộp file:

cat domain.key domain.crt ca_bundle.crt > haproxy.pem

Ví dụ:

cat example.com.key example.com.crt example.com.ca-bundle > /etc/haproxy/certs/example.pem

Cấu hình SSL trong HAProxy

Mở file cấu hình:

/etc/haproxy/haproxy.cfg

Thêm frontend HTTPS:

frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/example.pem
    mode http
    option httplog
    default_backend web_backend

Cấu hình backend

backend web_backend
    mode http
    balance roundrobin
    server web1 127.0.0.1:8080 check

Redirect HTTP sang HTTPS (khuyến nghị)

frontend http_front
    bind *:80
    redirect scheme https if !{ ssl_fc }

Kiểm tra và restart HAProxy

Kiểm tra config:

haproxy -c -f /etc/haproxy/haproxy.cfg

Restart:

systemctl restart haproxy

Kiểm tra SSL hoạt động

  • Truy cập: https://your-domain.com

  • Hoặc dùng lệnh:

openssl s_client -connect your-domain.com:443

Lỗi thường gặp & cách fix

❌ Lỗi không load được SSL

  • Sai thứ tự .pem

  • Thiếu private key

❌ Lỗi trình duyệt báo không an toàn

  • Thiếu CA bundle

❌ Key không khớp cert

Kiểm tra:

openssl rsa -noout -modulus -in domain.key | openssl md5
openssl x509 -noout -modulus -in domain.crt | openssl md5

Tổng kết

Việc cấu hình SSL trong HAProxy không quá phức tạp nếu bạn nắm rõ:

  • Cách tạo file .pem

  • Thứ tự key và certificate

  • Cấu hình frontend đúng chuẩn

Sau khi hoàn tất, hệ thống của bạn sẽ chạy HTTPS an toàn và chuyên nghiệp hơn 

haproxy ssl cấu hình https haproxy add ssl haproxy haproxy pem file cấu hình ssl linux reverse proxy ssl haproxy