ApacheにHSTS設定追加
せっかくウェブサイトを HTTPS 化したので、ついでに HSTS の設定も Apache に入れた。
HSTS(HTTP Strict-Transport-Security)って何? という方は MDN の解説をどうぞ。
1. 環境
- OS
- Linux
- Ubuntu 22.04
- Linux
- Webサーバ
- Apache 2.4.52
2. HSTS 設定追加
HTTPS の設定ファイルに以下の内容を記述する。
Header add Strict-Transport-Security "max-age=31536000"
あとは Apache を再起動すれば完了。
3. HSTS ヘッダ確認
ちゃんと設定できていればStrict-Transport-Securityというヘッダが HTTPS のヘッダにつくはずなので確認する。
$ curl https://site1.example.com -I
HTTP/1.1 200 OK
(中略)
Strict-Transport-Security: max-age=31536000
ちゃんとStrict-Transport-Securityが付くようになった。
4. ブラウザで確認
最後にブラウザでちゃんと HSTS が効いているか確認する。
わざとHTTP(not HTTPS)でhttp://site1.example.comにアクセスして、https://site1.example.comにアクセスしていたら OK 。