LoginSignup
1
2

More than 5 years have passed since last update.

ubuntu + nginx + Let's Encrypt でリバースプロキシを立てる話

Last updated at Posted at 2018-11-14

はじめに

  • とあるアプリケーションをオレオレ証明書でHTTPS化したが、オレオレ証明書では不具合あり。
  • ということであらためて、nginxとLet's Encryptでちゃんとした証明書を使ったリバースプロキシを構築する手順をメモ:writing_hand:

事前準備

  • サーバ(Ubuntu 18.04)

参考資料

Let's Encrypt で Nginx にSSLを設定する

letsencryptをUbuntu16.04 + Nginxで使ってみた

How To Secure Nginx with Let's Encrypt on Ubuntu 16.04

certbot公式

作業の流れ

  1. SSHログイン
  2. certbotのインストール
  3. nginxのバージョン確認
  4. certbotの実行
  5. nginxの再起動
  6. 動作テスト

【手順1】SSHログイン

  • 対象サーバにSSHでログインする

【手順2】certbotのインストール

  • 以下のコマンドを順に実行
# certbotとnginxをインストール
$ sudo apt-get install python-certbot-nginx

Note:
OSによってインストールコマンドが異なるのでUbuntuではない方はこちらからご確認ください。
https://certbot.eff.org/all-instructions

Note:
apacheの場合、パッケージ名が python-certbot-apache となります。

【手順3】nginxのバージョン確認

  • 以下のコマンドを順に実行
# nginxのバージョン確認
$ sudo apt show nginx

Note:
念の為の確認なので、この手順はスキップしても問題ありません。
Note:
【手順2】により、nginxもインストールされる

【手順4】certbotの実行

  • 以下のコマンドを順に実行
# certbotの実行
$ sudo certbot –-nginx -d <your hostname>
  • emailを入力
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): <your email address>
  • ライセンスにAgreeする。Aと入力。
-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A
  • メールを共有してよいか聞いてくる。Nと入力。
-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: N
  • <your hostname>に対して認証して、証明書を/etc/nginx/sites-enabled/defaultに格納してくれる
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for <your hostname>
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default
  • HTTPをHTTPSにリダイレクトしたいので、2と入力

Note:
nginxの設定ファイルを/etc/nginx/sites-enabled/defaultに自動作成してくれる
あとで修正したいときのために、覚えておくと便利。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/default
  • 成功すると以下メッセージが表示される
-------------------------------------------------------------------------------
Congratulations! You have successfully enabled
https://<your hostname>

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=<your hostname>
-------------------------------------------------------------------------------

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/<your hostname>/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/<your hostname>/privkey.pem
   Your cert will expire on 2019-01-31. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

【手順5】nginxの再起動

  • 以下のコマンドを順に実行
# nginxの再起動
$ sudo systemctl reload nginx

【手順6】動作テスト

  • Webブラウザでhttp://<your hostname>にアクセスする
  • うまくいくと、https://<your hostname>にリダイレクトされる。証明書もオレオレではないので、きちんと鍵マークで表示される。

まとめ

  • nginxでリバースプロキシを構築できました!
  • certbotで証明書の自動更新もできたりします。(追記予定)
  • リダイレクトの設定をゴニョゴニョいじりたい場合は、/etc/nginx/sites-enabled/defaultをチェック。nginx.confをいじってもダメなようです。うまくいかなくてハマりました。
  • /etc/nginx/sites-enabled/defaultの内容も備忘をメモを残します。(追記予定)
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2