nginx, ubuntu 16.04, Let's encrypt, webroot で SSL 導入したあと、http アクセスを https アクセスに転送する

やりたいこと

nginx, ubuntu 16.04, Let’s encrypt, webroot で SSL 導入したあと、http アクセスを https アクセスに転送する

LB 挟まず、1台に複数のアプリケーションをのせている開発用の小さい環境での設定です。

環境

http から https への転送

1. 設定ファイルをエディタで開く

sudo vi /etc/nginx/sites-available/default

2. 80番を受け付けている server の server_name を消して location / ブロックに 301 で https アクセスし直すよう設定

下記で設定の場合です

  • ドメイン yourdomain.example.com
  • http アクセスは https に転送
  • yourdomain.example.com に来たら 9000番に転送
  • できればそのまま letsencrypt renew は動く状態にしたい # これは未確認。早めに確認しないと。。。
server {
  listen 80 default_server;
  root /var/www/html;
  location ~ /.well-known {
    allow all;
  }
  location {
    return 301 https://$server_name$request_uri;
  }
}
server {
  server_name yourdomain.example.com;
  listen 443 ssl;
  include snippets/ssl-yourdomain.example.com.conf;
  include snippets/ssl-params.conf;
  location / {
    proxy_pass http://localhost:9000;
  }
}

3. reload

sudo nginx -t # テスト 心配なら
sudo systemctl reload nginx

で大丈夫なはず . . 多分。