Cấu hình HTTPS Server cho NGINX (SSL Certificate trong NGINX)

Bài viết được sự cho phép của tác giả Trần Hữu Cương

Cấu hình HTTPS Server cho NGINX (SSL Certificate trong NGINX)

Trong bài này mình sẽ hướng dẫn mở tính năng HTTPs trên NGINX. (Mặc định thì NGINX đang tắt tính năng này)

  502 bad gateway nginx - Xử lý lỗi "502 Bad Gateway" trên Nginx
  Cách Engineer Nhật Bản thực hiện test như thế nào

Cấu hình HTTPS Server cho NGINX (SSL Certificate trong NGINX)

Mở file nginx.conf tìm tới phần HTTPS server

# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

Mặc định NGINX không mở HTTPS, để enable HTTPS ta bỏ comment đoạn trên bằng cách xóa đi các dấu # ở đầu

Ví dụ:

# HTTPS server

server {
 listen 443 ssl;
 server_name localhost;

 ssl_certificate cert.pem;
 ssl_certificate_key cert.key;

 ssl_session_cache shared:SSL:1m;
 ssl_session_timeout 5m;

 ssl_ciphers HIGH:!aNULL:!MD5;
 ssl_prefer_server_ciphers on;
 location / {
 root html;
 index index.html index.htm;
}
}

Theo như cấu hình trên thì server sẽ thực hiện mã hóa ssl khi truy cập qua cổng 443, ssl certificate được sử dụng là file cert.pem và cert.key (nằm cùng folder với file nginx.conf)

Ngoài ra bạn cũng có thể trỏ ssl certificate tới địa chỉ trên mạng, ví dụ:

ssl_certificate www.example.com.chained.crt;
ssl_certificate_key www.example.com.key;

(Xem lại: Hướng dẫn tạo certificate SSL trên ubuntu (file key, pem))

Kết quả: truy cập vào địa chỉ https://localhost:443

Cấu hình HTTPS Server cho NGINX (SSL Certificate trong NGINX)

Okay, Done!

References: http://nginx.org/en/docs/http/configuring_https_servers.html

Bài viết gốc được đăng tải tại stackjava.com

Có thể bạn quan tâm:

Xem thêm Việc làm Developer hấp dẫn trên TopDev