Cấu hình HTTP Strict Transport Security (HSTS)

Ngày đăng: 2015-07-23 11:39:41 | Cập nhật: 2015-07-23 11:53:32

HTTP Strict Transport Security (thường được viết tắt là HSTS) là một tính năng bảo mật cho phép một trang web thông báo cho các trình duyệt chỉ nên giao tiếp bằng giao thức HTTPS thay vì HTTP. Bài viết này sẽ hướng dẫn cho bạn cách thiết lập HSTS trong Apache2, Nginx và Lighttpd. Chúng tôi đã thử nghiệm với nginx 1.1.19, Lighthttpd 1.4.28, Apache 2.2.22 trên Ubuntu 12.04, Debian 6 & 7 và CentOS 6.

 

Apache:

Bạn thêm đoạn sau vào cấu hình trong Virtual Host trên port 443:

# Optionally load the headers module:
LoadModule headers_module modules/mod_headers.so

<VirtualHost x.x.x.x:443>
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
</VirtualHost>

Bạn cũng đừng quên cấu hình việc tự chuyển từ HTTP sang HTTPS trong cấu hình Virtual Host trên port 80:

<VirtualHost *:80>
  [...]
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
  </IfModule>
</VirtualHost>

Sau đó restart lại Apache.

 

Lighthttpd:

Bạn bổ sung đoạn sau vào file cấu hình của Lighthttpd (ví dụ /etc/lighthttpd/lighthttpd.conf):

server.modules += ( "mod_setenv" )
$HTTP["scheme"] == "https" {
    setenv.add-response-header  = ( "Strict-Transport-Security" => "max-age=63072000; includeSubdomains; preload")
}

Sau đó restart lại Lighthttpd.

 

Nginx:

Bạn bổ sung đoạn code sau vào file cấu hình của Nginx (ví dụ /etc/nginx/nginx.conf):

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

 Sau đó restart lại Nginx.