LoginSignup
16
18

More than 5 years have passed since last update.

ワイルドカード CNAME と Apache の vhost_alias でバーチャルホストを作る

Posted at

自分が example.net ドメインの所有者だとして、次のようにレコードを登録します。

example.net.zone
www             IN      A       192.0.2.123
*               IN      CNAME   www

Apache で vhost_alias モジュールを有効にします。

httpd.conf
LoadModule vhost_alias_module modules/mod_vhost_alias.so

さらに、次のようにバーチャルホストを設定します。

httpd.conf
<VirtualHost *:80>
        ServerName www.example.net
        ServerAlias *.example.net

        VirtualDocumentRoot /var/www/vhost/%1/html

        LogFormat "%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vcombined
        CustomLog logs/vhost_access_log vcombined

        <Directory /var/www/vhost>
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

バーチャルホストごとのドキュメントルートを作ります。

$ mkdir -p /var/www/vhost/aaa/html
$ mkdir -p /var/www/vhost/bbb/html
$ echo aaa > /var/www/vhost/aaa/html/index.html
$ echo bbb > /var/www/vhost/bbb/html/index.html

ディレクトリを作るだけでバーチャルホストになります。

$ curl aaa.example.net
aaa
$ curl bbb.example.net
bbb
16
18
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
16
18