あまブログ

ドキドキ......ドキドキ2択クイ〜〜〜〜〜〜〜ズ!!

【Debian】NginxでVirtual Hostsを使って複数のドメインを設定する方法【Server Blocks】

NginxのWebサーバではVirtual Hosts(Server Blocks)を使って1台のサーバで複数のドメインを運用することができます。

この記事では、Debian 11上のNginxにVirtual Hostsを設定する方法を紹介します。

なお、本稿の手順を進める前にDebian上にNginxがインストールされている必要があります。

DebianでのNginxのインストール方法は以下の記事を参照してください。

ama-tech.hatenablog.com

環境

  • Debian 11 bullseye
  • nginx 1.21.6

手順

1. ドキュメントルートディレクトリの作成

ドキュメントルートは各ドメインのhtmlファイルが保存されるディレクトリで、任意の場所に作ることができます。

以下では /var/wwwディレクトリの中にサーバでホストしたいドメインごとに html ディレクトリを作成します。

今回はサンプルとしてtest1.comtest2.comの2つのドメインを使用します。

以下のコマンドを実行して、ドキュメントルートディレクトリを作成します。

$ sudo mkdir -p /var/www/test1.com/html
$ sudo mkdir -p /var/www/test2.com/html

2. サンプルページの作成

次に各サイトのデフォルトページをドキュメントルートディレクトリの中に作成します。

以下のコマンドを実行して、1つ目のサンプルページを作成します。

$ sudo vi /var/www/test1.com/html/index.html

テスト用として、以下の内容で作成します。

/var/www/test1.com/html/index.html

<html>
    <head>
        <title>test1</title>
    </head>
    <body>
        <h1>test1.com</h1>
    </body>
</html>

続けて、2つ目のサンプルページも作成します。

$ sudo cp /var/www/test1.com/html/index.html /var/www/test2.com/html/
$ sudo vi /var/www/test2.com/html/index.html

内容は以下のようになります。

/var/www/test2.com/html/index.html

<html>
    <head>
        <title>test2</title>
    </head>
    <body>
        <h1>test2.com</h1>
    </body>
</html>

3. Server Blocksファイルの作成

次にNginxでVirtual Hostsを使うための設定ファイル(Server Blocksファイル)を作成します。

各ドメイン毎にServer Blocksファイルを作成します。

以下のコマンドを実行して、1つ目のServer Blocksファイルを作成します。

$ sudo vi /etc/nginx/sites-available/test1.com

内容は以下のようになります。

/etc/nginx/sites-available/test1.com

server {
        listen 80;

        root /var/www/test1.com/html;
        index index.html;

        server_name test1.com www.test1.com;

        access_log /var/log/nginx/test1.com.access.log;
        error_log /var/log/nginx/test1.com.error.log;
}

上記の設定では、test1.comwww.test1.comに対するリクエストに応答し、ドキュメントルートの/var/www/test1.com/htmlにあるindex.htmlを表示します。

また、アクセスログを/var/log/nginx/test1.com.access.logに、エラーログを/var/log/nginx/test1.com.error.logに保存します。

続けて、2つ目のServer Blocksファイルを作成します。

$ sudo cp /etc/nginx/sites-available/test1.com /etc/nginx/sites-available/test2.com
$ sudo vi /etc/nginx/sites-available/test2.com

内容は以下のようになります。

/etc/nginx/sites-available/test2.com

server {
        listen 80;

        root /var/www/test2.com/html;
        index index.html;

        server_name test2.com www.test2.com;

        access_log /var/log/nginx/test2.com.access.log;
        error_log /var/log/nginx/test2.com.error.log;
}

4. Server Blocksファイルの有効化

最後にServer Blocksファイルの有効化を行います。

Nginxでは設定ファイルは/etc/nginx/sites-availableディレクトリに保存され、/etc/nginx/sites-enabledディレクトリに設定ファイルへのシンボリックリンクを作成します。

/etc/nginx/sites-availableディレクトリには現在有効になっているかどうかに関わらず、全てのVirtual Hostsの設定を保存します。

一方/etc/nginx/sites-enabledディレクトリには、現在有効になっているドメインのみ、設定ファイルのシンボリックリンクを作成します。

これにより選択的にVirtual Hostsを有効化することができます。

以下のコマンドを実行して、ドメインごとのシボリックリンクを作成します。

$ sudo ln -s /etc/nginx/sites-available/test1.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/test2.com /etc/nginx/sites-enabled/

続けて、/etc/nginx/nginx.confを編集します。

$ sudo vi /etc/nginx/nginx.conf

/etc/nginx/nginx.confに以下を追加して、Nginxが起動時に/etc/nginx/sites-enabledディレクトリを読み込むように設定します。

include /etc/nginx/sites-enabled/*;

以下を実行して、設定ファイルのテストを行います。

$ sudo nginx -t

問題がなければ、nginxを再起動して設定の変更を反映させます。

$ sudo systemctl restart nginx

以上で設定は終了です。

ブラウザでドメインにアクセスして作成したサンプルページが表示されればOKです。


【参考】