[Nginx] http を https にリダイレクトする設定 & HTTP Strict Transport Security (HSTS) 対応
Nginx で http リクエストを https にリダイレクトする設定と HTTP Strict Transport Security (HSTS) の設定の2つをご紹介します。
http を https へリダイレクトする設定
以下のように return 301 https://$host$request_uri; の部分で 301 リダイレクトさせてます。
server { listen 80; server_name example.com; return 301 https://$host$request_uri; } server { # 'http2' requires nginx 1.9.5+. If using older nginx, replace with 'spdy'. listen 443 ssl http2; server_name example.com; } |
HTTP Strict Transport Security (HSTS) の設定
server { listen 443 ssl http2; # HTTP Strict Transport Security: tells browsers to require https:// without first checking # the http:// version for a redirect. Warning: it is difficult to change your mind. # # max-age: length of requirement in seconds (31536000 = 1 year) # includeSubdomains: force TLS for *ALL* subdomains (remove if this is not what you want) # preload: indicates you want browsers to ship with HSTS preloaded for your domain. # # Submit your domain for preloading in browsers at: https://hstspreload.appspot.com add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; # If you won't/can't turn on HTTPS for *all* subdomains, use this simpler version: # add_header Strict-Transport-Security "max-age=31536000;" always; } |
HSTS Preload リストへ登録する
Google では ドメインを HSTS Pre-loaded List に登録するサービス を行っています。記載された手順に従ってドメインを登録すれば、今後ブラウザとそのドメインとの間に安全でない通信は起こらなくなります。これに登録するには Strict-Transport-Security ヘッダに preload パラメータを含める必要があります。
とのことなので HSTS Preload List Submission から登録しましょう。
HSTS Preload List を登録するための条件として、
- サブドメインは指定できないのでネイキッドドメインを指定する
- add_header で includeSubDomains を設定する
などが必要です。
今の時代 GoogleのHTTPSサイト優遇方針で待ったなし! と言われていることもあり、未対応のサイト運営者は早めに対応したほうがよさそうです。
参考情報
- nginxで常時SSL(https)通信になるように設定する – note.technology
- nginx TLS / SSL configuration options for konklone.com
- HTTP Strict Transport Security (HSTS) and NGINX – NGINX
- HTTPS でサイトを保護する – Search Console ヘルプ