Nowインスタンスに独自ドメインを設定する

PaaS の Now は無料プランでも独自ドメインを設定できます.Heroku も然り.

Now の中でドメインを買うこともできますが,予め購入済みであればそれを適用することもできます.今回は,お名前.com で購入してある独自ドメイン (サブドメイン) を使います.

Now パッケージのインストール

terminal

$ npm i -g now

エイリアス設定

動的に変化する URL はエイリアスを指定することで固定できます.この場合,URL は https://<example>.now.sh に固定できます.

terminal

$ now alias https://example-xxxxxxxxx.now.sh example
> Success! example.now.sh now points to example-xxxxxxxxx.now.sh

エイリアス設定は独自ドメインにも適用できます.

ドメイン設定

メインドメインの追加

次のコマンドでメインドメインを追加できます.サブドメインを追加するにあたってもこの手順を踏む必要があります.

terminal

$ now domains add example.com
> Success! Domain example.com added correctly. [2s]
> WARN! The domain was added but it is not verified. To verify it, you should either:
 a) Change your domain nameservers to the following intended set: [recommended]

    Intended Nameservers    Current Nameservers
    a.zeit-world.co.uk      01.example-dns.com              <span class="red">☓</span>
    b.zeit-world.org        02.example-dns.com              <span class="red">☓</span>
    d.zeit-world.net        03.example-dns.com              <span class="red">☓</span>
    f.zeit-world.com        04.example-dns.com              <span class="red">☓</span>

 b) Add a DNS TXT record with the name and value shown below.

  <code> name        type        value</code>
  <code> _now        TXT         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</code>

 We will run a verification for you and you will receive an email upon completion.
 If you want to force running a verification, you can run `now domains verify`
 Read more: https://err.sh/now-cli/domain-verification

ドメインの認証をするための警告メッセージが表示されます.

  • a) ネームサーバを zeit のものに変更する
  • b) TXT レコードを利用する

今回はネームサーバを動かさずに登録していくので,b の value を DNS でホスト名 _now の TXT レコードに登録します.また,サブドメインの CNAME レコードに alias.zeit.co. を登録します.ちなみに,お名前.com のネームサーバなら最後のピリオドは不要です.

ドメインの所有者認証

(反映まで間があるので) ネームサーバを更新してから少し時間を空けて,以下のコマンドを実行します.メインドメインが登録されているネームサーバに確認しに行くので,ドメインとして指定するのはメインドメインです.

terminal

$ now domains verify example.com
> Success! Domain myproject.com was verified using DNS TXT record. [500ms]
  You can verify with nameservers too. Run `now domains inspect example.com` to find out the intended set.

サブドメイン登録

認証が終了したら,エイリアスをサブドメインに設定します.

terminal

$ now alias https://example-xxxxxxx.now.sh sub.example.com

終.

リダイレクト設定

myproject.com を www.myproject.com にリダイレクトするときなどは設定が必要です.詳しくは以下をご覧ください.

https://jpn.now.sh/#独自ドメインの設定

最後に

Heroku でも独自のメインドメイン,サブドメインを使うことができますが,Now の何が良かったかというと独自ドメインでもドメイン証明書を発行できることです!(Heroku は $7/month/dyno)

https://zeit.co/docs/v2/domains-and-aliases/zero-downtime-domain-migration/