さくら VPS の前段にウェブアクセラレータを置いて Let's Encrypt を設定する

September 20, 2019

さくらの Web アクセラレータは無料枠のある CDN サービスです。これをさくら VPS の Nginx で運用するサイトの前段において、Let’s Encrypt で SSL 化したときの手順をまとめました。

Let’s Encrypt は certbot ツールが便利なのでこちらをインストールします。

SSL 証明書の発行には、ドメイン認証のために http 経由で http://対象ドメイン/.well-known/acme-challenge/ 下にあるワンタイムトークンを取得できるようにする必要があります。このパスは、https 化した後に http アクセスをリダイレクトする場合も除外するようにしておかないといけません。

Let’s Encrypt で証明書を発行

ルートディレクトリの設定

対象ドメインのルートディレクトリを作成します。

1
# mkdir /var/www/example.tilfin.net

Nginx の設定

対象ドメインの server (http) ディレクティブ内に、下記の location ディレクティブを追加します。Nginx をリロードします。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name example.tilfin.net;

...

location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/example.tilfin.net;
break;
}

...
}

certbot で証明書を発行

1
2
3
4
5
6
7
8
9
10
# certbot certonly -d "example.tilfin.net" -w /var/www/example.tilfin.net
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel):

ここで 3 を入力します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.tilfin.net
Using the webroot path /var/www/example.tilfin.net for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.tilfin.net/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.tilfin.net/privkey.pem
Your cert will expire on 2019-12-18. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- 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

となれば成功です。

ウェブアクセラレータの設定

初期設定

さくらのクラウドの「ウェブアクセラレータ コントロールパネル」を開いて、サイトを追加します。
ドメイン種別は「独自ドメイン」にして、公開ドメインとホストヘッダは上記と同じドメインにします。リクエストプロトコルは「http/https」にして、オリジンプロトコルは「http」にします。

登録されたサイトの設定画面を開きます。公開ドメインを指定された xxxx.user.webaccel.jp ドメインへの CNAME レコードとして設定します。サイトのステータスを 有効 にします。

SSL 証明書の登録

ターミナルで cat /etc/letsencrypt/live/example.tilfin.net/fullchain.pem して中身を証明書欄に貼り付けます。ターミナルで cat /etc/letsencrypt/live/example.tilfin.net/privkey.pem して中身を秘密鍵欄に貼り付けます。保存します。

さくら VPS さくらウェブアクセラレータ

tilfin freelance software engineer