LoginSignup
5
4

More than 3 years have passed since last update.

一つのサーバーで複数のドメインを利用する

Last updated at Posted at 2019-12-01

はじめに

この記事は ConoHa Advent Calendar 2019 4日目 です。
本当は去年書きたかった内容です。

1.本記事の目的

ある日、ふと思った。

複数のドメインを取得することになったものの、
それぞれのドメイン別サイトへのアクセスは軽いために、一つのサーバーで複数のドメインを利用したい。

やりたいこと
(1)ドメインごとにアクセス先のディレクトリを指定する・別サイトに転送する
    将来的に2つのドメインのアクセス先ディレクトリを一つにする。(サイトの統合)
    別の2つのドメインにきたものは、それぞれ別サーバーのドメインに転送
    IPアドレスに直接アクセスした場合は特定のディレクトリにアクセスさせる(SSL化しない)

(2)SSLの証明書を適用する
    3ドメインをSSL対応する

簡単な解決方法はサーバーを複数借りてしまえばいいものの、一つで対応する方法はないものか。

図にするとこのような形
pic1.png

2.実験環境

VPS 512MB CPU1Core/SSD20GB
一番安いもの

OSはDebian

3.解決方法

簡単に書くと、全て
https.confVirtualHostを書き足す
ことで解決する

(1)ドメインごとにアクセス先のディレクトリを指定する・別サイトに転送する**

 将来的には2つのドメインのアクセス先ディレクトリを一つにする。(サイトの統合)

 example.jp と example.net と203.0.113.0 をそれぞれ異なるディレクトリを指定する
 example.info と example.tokyo を別のドメインに転送する
 

conf自体は、実際の運用方法で記述しています。

http.conf
############  example.jp を /var/www/html/jp/ とする########################

<VirtualHost *:80>
ServerName example.jp:80
DocumentRoot /var/www/html/jp/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.jp
  RewriteRule ^/(.*)$ https://example.jp/$1 [R=301,L]

<Directory /var/www/html/jp/>
Allow from all
</Directory>
</VirtualHost>

############  example.net を /var/www/html/net/ とする########################

<VirtualHost *:80>
ServerName example.net:80
DocumentRoot /var/www/html/net/

##将来的に example.jp と example.net を統合する場合は
##DocumentRoot /var/www/html/net/ を DocumentRoot /var/www/html/jp/ にする

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.net
  RewriteRule ^/(.*)$ https://example.net/$1 [R=301,L]

<Directory /var/www/html/net/>
Allow from all
</Directory>
</VirtualHost>


############  example.info を /var/www/html/info/ とする・https://hoge.jpに転送する########################

<VirtualHost *:80>
ServerName example.info:80
DocumentRoot /var/www/html/info/



RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.info
  RewriteRule ^/(.*)$ https://example.info/$1 [R=301,L]

RewriteEngine On
  RewriteRule index\.html$ http://hoge.jp [R=301,L]

<Directory /var/www/html/info/>
Allow from all
</Directory>
</VirtualHost>

############  example.tokyo を /var/www/html/tokyo/ とする・https://hogehoge.jpに転送する(非SSL化)########################

<VirtualHost *:80>
ServerName example.tokyo:80
DocumentRoot /var/www/html/tokyo/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.tokyo
  RewriteRule ^/(.*)$ http://example.tokyo/$1 [R=301,L]

RewriteEngine On
  RewriteRule index\.html$ http://hogehoge.jp [R=301,L]



<Directory /var/www/html/tokyo/>
Allow from all
</Directory>
</VirtualHost>


############  203.0.113.0 を /var/www/html/302/ とする ########################

<VirtualHost *:80>
ServerName 203.0.113.0:80
DocumentRoot /var/www/html/203/
<Directory /var/www/html/203/>
Allow from all
</Directory>
</VirtualHost>

(2)SSLの証明書を適用する

 SSL化は example.jp と example.net と example.info とする

前提
 let's encrypt を利用するとして、ドメインごとに証明書を取得している
 example.jp 用の証明書は /etc/letsencrypt/live/example.jp/ に保存される
 example.net 用の証明書は /etc/letsencrypt/live/example.net/ に保存される
 example.info 用の証明書は /etc/letsencrypt/live/example.info/ に保存される

ssl.conf

############  example.jp のSSL化########################
<VirtualHost *:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn
  ServerAdmin root@example.jp
  DocumentRoot /var/www/html/jp
  ServerName example.jp
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.jp/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.jp/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.jp/chain.pem
<Directory "/var/www/html/jp">
AllowOverride All
</Directory>
</VirtualHost>

############  example.net のSSL化########################

<VirtualHost *:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn
  ServerAdmin root@example.net
  DocumentRoot /var/www/html/net
  ServerName example.net
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.net/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.net/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.net/chain.pem
<Directory "/var/www/html/net">
AllowOverride All
</Directory>
</VirtualHost>

############  example.info のSSL化########################

<VirtualHost *:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn
  ServerAdmin root@example.info
  DocumentRoot /var/www/html/info
  ServerName example.net
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.info/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.info/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.info/chain.pem
<Directory "/var/www/html/info">
AllowOverride All
</Directory>
</VirtualHost>

権限などは、かなりいい加減にしています。

おわりに

一つのサーバーしか利用していないような記事になってしまいましたが、実際には5つほど利用です。
サーバー容量として20GBは少ないように感じますが、オブジェクトストレージを繋いでいるので、この運用でも特に困る事は今のところないです。

let's encryptも、ワイルドカードが対応しているようですので、少しずつどう対応させるか考えています。

来年は 買えるなら株買って、株主優待でConoHaを少し安く使ってみた という記事も書いてみたい。

別件、先日行われた、このはぴば も私、行きました。思ったことです。
来年、このはぴば があれば、 このは飴ちゃん か このはガム をお願いします。!!!

5
4
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
5
4