Apache Virtual Host設定でサブドメインごとに異なる設定を行う方法は?

このQ&Aのポイント
  • Apache(2.0.63)を使用して、name basedなvirtual host設定を行っていますが、特定のサブドメインに対して異なる設定を適用したい場合、どのようにすれば良いでしょうか?
  • Apache(2.0.63)のname basedなvirtual host設定では、例えばexample.comやwww.example.com、develop.example.comなどのサブドメインに対してはそれぞれ独自の設定が適用されますが、想定していないサブドメイン(例えば<想定していないサブドメ>.example.com)に対して特定の設定を行いたい場合、どのようにすれば良いでしょうか?
  • Apache(2.0.63)のname basedなvirtual host設定において、特定のサブドメイン(<想定していないサブドメ>.example.com)に対して異なる設定を適用する方法を教えてください。
回答を見る
  • ベストアンサー

Apache(2.0.63)(name basedなvirtual host)で、<想定していないサブドメ>.example.com に対してそれ専用の設定を。

Apache(2.0.63)で、name basedなvirtual hostをおこなっているのですが、example.com www.example.com develop.example.com の場合は、 それぞれのvirtual hostごとの設定(DocumentRoot など)がよまれ、【<想定していないサブドメ>.example.com】 の場合は、その場合特有の設定(DocumentRoot など)が読まれるようにしたいのですが、どうすればいいのでしょうか? <VirtualHost _default_:*>ほげほげ</VirtualHost>はIPベースのときにしか意味ないようですし・・・ なお。以下のように、ServerName をかかなければいい、というのをどこかでみかけ、実践したのですが、挙動変わらずです。 <VirtualHost *:80> # ServerName wildcard.example.com DocumentRoot /var/www/wildcard.example.com/htdocs </VirtualHost> DNSのほうは、 www 111.111.11.111 @ (None) 111.111.11.111 * (All Others) 111.111.11.111 develope 111.111.11.111 また、httpd.conf のほうは、 NameVirtualHost *:80 をちゃんと書いています。

質問者が選んだベストアンサー

  • ベストアンサー
  • t-okura
  • ベストアンサー率75% (253/335)
回答No.1

HOST ヘッダに対応するバーチャルホストの設定がないときは、最初の バーチャルホストが使われます。wildcard.example.com 用の設定を 他のバーチャルホストの設定より前(つまり先頭)にもって行きましょう。 また、ServerName は、wildcard.example.com など、他のバーチャルホスト で使用していない名前を設定したほうがよいのではないでしょうか (省略可能かどうかは知りません)。

関連するQ&A

  • ApacheでのVirtual Hostの設定

    RedHatLinux9.0/Apache2.0 動的IPのADSLでhn.orgのDDNSを使い、サーバを立てました。 ドメインを持っているため、それを割り当てようと/etc/httpd/conf/httpd.confでVirtual Hostの設定をしたのですが、外部からドメインで接続しても指定したディレクトリを読みに行ってくれず、ディフォルトの/var/www/htmlのディレクトリが表示されていまいます。 DNS(bind?)をこちらで動かしてないのですが、それが原因なのでしょうか? httpd.confはこのようにしています。 # # Use name-based virtual hosting. # NameVirtualHost 127.0.0.1 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # <VirtualHost 127.0.0.1> ServerAdmin admin@自分のドメイン.biz DocumentRoot /home/ff/www/ ServerName www.自分のドメイン.biz ServerAlias 自分のドメイン.biz ErrorLog logs/ff/error_log CustomLog logs/ff/access_log common </VirtualHost> また、NameVirtualHost 127.0.0.1と<VirtualHost 127.0.0.1>を、NameVirtualHost *と<VirtualHost *>に変えるとすべてのアクセスがForbiddenになってしまいます。 ログをみたところ、どういうわけかIE6.0なのにHTTP/1.0で繋がっているみたいなのですが、やはりそれなのでしょうか・・・? すみません。アドバイスよろしくお願いいたします。m(_ _)m

  • Apache Virtual Hostでの認証

    Apache の Virtual Host で認証をかけることはできますか? 以下のように設定したのですが、Apache起動時に Syntax error on line 1066 of /usr/local/apache2/conf/httpd.conf: AuthType not allowed here といわれます。 .htacessを使えば実現できるようですが、VirtualHostディレクティブ内で設定する方法はないでしょうか? <VirtualHost *:80> ServerAdmin test@example.com DocumentRoot /home/test ServerName test.example.com ErrorLog logs/test-error_log CustomLog logs/test-access_log common AuthType Basic AuthName "Restricted Files" AuthUserFile /home/test/.htpasswd Require user test </VirtualHost> よろしくお願いします。

  • apacheのバーチャルホストの設定方法

    MacOSX Snow Leopardを使っています。 apacheは2.2.11です。 httpd.confで/http/Virtual.confを読み出して設定しています。 Virtual.conf ------- # #VirtualHost.conf # NameVirtualHost *:80 <VirtualHost *:80> ServerName www.xxxxx.com DocumentRoot /http/www.xxxxx.com </VirtualHost> ------- ※www.xxxxx.comは、正しく設定されています。 の設定をし、httpdを再起動したところ、IPアドレスでアクセスしても、ドメインでアクセスしても、httpコード403になってしまいます。 *httpdで起動時、エラーはでません。 *httpd -tはSyntaxOK *httpd -sは   VirtualHost configuration:   wildcard NameVirtualHosts and _default_ servers:   *:80 is a NameVirtualHost    default server www.xxxxx.com (/http/Virtual.conf:11)    port 80 namevhost www.xxxxx.com (/http/Virtual.conf:11)   Syntax OK です。 virtual.confをすべてコメントアウトしたところしっかりアクセスできました。 よろしくお願いします。

  • パーパーフェクトPHP[技術評論社]のP29まで終

    パーフェクトPHP[技術評論社]のP29まで終わりました。 次に、P30に入りました。そこで! 以下教本の中身です。 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ■バーチャルホストの追加手順  バーチャルホストを追加する際はまずドキュメントルートとなるデレクトリを作成します。 C:\xampp\apache\conf\extra\httpd-vhosts.confにバーチャルホストの設定を追加します。 <VirtualHost>で囲まれた部分をコピーして、ServerNameやDocumentRootの値を書き かえて保存した後、Apacheを再起動します。また、必要に合わせて C:\Windows\System32\drivrs\etc\hostsにドメインの指定を追加します。  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  以上ですが、  「<VirtualHost>で囲まれた部分をコピーして、ServerNameやDocumentRootの値を書き かえて保存します」  とありますが! 「※何をやるのか!意味がまったく分かりません!?」  以下が、C:\xampp\apache\conf\extra\httpd-vhosts.conf  のファイルの中身の 「ServerNameやDocumentRootの値を書き かえ」は  教本・パーフェクトPHP P28の「ドキュメントルートの設定」 で、既に終了しているのでは!ないでしょうか!? 素人の考えですが、「※間違いでしょうか」  ・・\httpd-vhosts.confの  ファイルの現在のプログラムの中身です。宜しくお願いします。 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  # Virtual Hosts # # Required modules: mod_log_config # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.4/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 <VirtualHost *:80> ServerName study.localhost DocumentRoot "C:/xampp/htdocs/study.localhost" DirectoryIndex index.php index.html <Directory "C:/xampp/htdocs/study.localhost"> AllowOverride All Allow from All </Directory> </VirtualHost> ##NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ##ServerName or ##ServerAlias in any <VirtualHost> block. # ##<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host.example.com" ##ServerName dummy-host.example.com ##ServerAlias www.dummy-host.example.com ##ErrorLog "logs/dummy-host.example.com-error.log" ##CustomLog "logs/dummy-host.example.com-access.log" common ##</VirtualHost> ##<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host2.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host2.example.com" ##ServerName dummy-host2.example.com ##ErrorLog "logs/dummy-host2.example.com-error.log" ##CustomLog "logs/dummy-host2.example.com-access.log" common ##</VirtualHost>

    • ベストアンサー
    • PHP
  • PHPの教本の説明などで質問があります。

    http://www.amazon.co.jp/dp/4774144371  以上教本の、P28~P29の質問です。  以下中身です。 ■ドキュメントの設定 ドキュメントルールをC:\xampp\htdocs\study.localhostに設定します。まずはディレクトリを作成します。  次にApacheのバーチャルホストを設定します。C:\xampp\apache\conf\conf\extra\http-vhost.conf を開き次の設定を追加してください。<VirtualHost>で囲まれた部分がバーチャルホストの設定です。ServerNameにWeb ブラウザからアクセスする際のドメイン名(study.localhost)、DocumentRootおよび<Directory>にドキュメントルートへの パスを指定します。 (パソコン側のファイルを以下のプログラムに修正しろと言うことでしょう!?)  NameVirtualHost *:80 <Virtualhost *80> ServerName study.localhost DocumentRoot C:\xampp\htdocs\study.localhost DirectoryIndex index.php index.html <Directory “C:\xampp\htdocs\study.localhost”> AllowOverride All Allow from All </Directory> </VirtualHost> ---------------------------------------------------------- 以上が教本の中身です。 そこで質問があります! 第一の質問  教本では C:\xampp\apache\conf\conf\extra\http-vhost.confですが! パソコン側に存在するファイルは httpd-vhosts.confです。 教本のhttp-vhost.confではなくて、httpの部分がパソコン側では httpdと成っていて、dが多いです! ファイル名が違うことで、設定後何か支障はありますか!? 第二の質問  以下が、httpd-vhosts.conf  ファイルのプログラム全文を以下表示しましたが、 ##NameVirtualHost *:80が二個存在していて、どちらのプログラムを 修正すればいいのでしょうか!?  以上二件よろしくお願いします。 # Virtual Hosts # # Required modules: mod_log_config # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.4/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # ##NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ##ServerName or ##ServerAlias in any <VirtualHost> block. # ##<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host.example.com" ##ServerName dummy-host.example.com ##ServerAlias www.dummy-host.example.com ##ErrorLog "logs/dummy-host.example.com-error.log" ##CustomLog "logs/dummy-host.example.com-access.log" common ##</VirtualHost> ##<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host2.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host2.example.com" ##ServerName dummy-host2.example.com ##ErrorLog "logs/dummy-host2.example.com-error.log" ##CustomLog "logs/dummy-host2.example.com-access.log" common ##</VirtualHost>

    • 締切済み
    • PHP
  • apache2.2でのバーチャルホストでの設定

    質問させて頂きます。 apache2.2.21にてweb鯖を立てて、ドメインを取得しDDNSにてテストしています。 test.comというドメインにバーチャルホストにて web1.test.comとweb2.test.comを設定 した場合に、 なぜかtest.comとweb1.test.comが同じ内容が表示されてしまいます。 web2は通常通り表示されます。 詳しい方が居ましたら間違い点を教えて下さい。 httpd-vhostsの内容 # # Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # <VirtualHost *:80> DocumentRoot "C:/web/web1" ServerName web1.babayasu.info ErrorLog "logs/web1.localhost-error.log" CustomLog "logs/web1.localhost-access.log" common <Directory "C:/web/web1"> order deny,allow allow from ALL Options Indexes FollowSymLinks ExecCGI IncludesNoExec </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "C:/web/web2" ServerName web2.babayasu.info ErrorLog "logs/web1.localhost-error.log" CustomLog "logs/web1.localhost-access.log" common <Directory "C:/web/web2"> order deny,allow allow from ALL </Directory> </VirtualHost>

  • 〆た質問にリンクを張り、仕切り直しの質問です。

    http://okwave.jp/qa/q8351280.html  のご回答等・・でお世話になっております^^ 私なりに頭の中で”紆余曲折”した、質問でした。    私みたいなPHP二年生ですが、  なんでもそうだと思うのですが! “素人というのは”とんでもない勘違いを良くしています! 私もその一人です。  なので、自分だけの考えだけで、事を進められないので苦労しています。   ご回答を下さった、 “「#」が先頭についているのは全部コメントです。” 意味が分かりました^^  現在のプログラムの設定を、とりあえず以下の設定にしました。 教本には「http-vhosts.confファイルを開き追加してください。」  とありましたので追加しましが、間違いないでしょうか!?    まだどこか勘違いをしているような気がします。  ※ 以上URLから何か、新たにどこかのフォルダ内にファイルを追加する!    との、回答の内容と、受け止められました。等・完璧ではございません    !勘違いなのでしょうか!?等・・   何か全体的に大きな勘違いをしているようでしたらご教授をよろしくお願い致します。 # Virtual Hosts # # Required modules: mod_log_config # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.4/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 <VirtualHost *:80> ServerName study.localhost DocumentRoot "C:/xampp/htdocs/study.localhost" DirectoryIndex index.php index.html <Directory "C:/xampp/htdocs/study.localhost"> AllowOverride All Allow from All </Directory> </VirtualHost> ##NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ##ServerName or ##ServerAlias in any <VirtualHost> block. # ##<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host.example.com" ##ServerName dummy-host.example.com ##ServerAlias www.dummy-host.example.com ##ErrorLog "logs/dummy-host.example.com-error.log" ##CustomLog "logs/dummy-host.example.com-access.log" common ##</VirtualHost> ##<VirtualHost *:80> ##ServerAdmin webmaster@dummy-host2.example.com ##DocumentRoot "C:/xampp/htdocs/dummy-host2.example.com" ##ServerName dummy-host2.example.com ##ErrorLog "logs/dummy-host2.example.com-error.log" ##CustomLog "logs/dummy-host2.example.com-access.log" common ##</VirtualHost>

    • ベストアンサー
    • PHP
  • Apache VirtualHost でワイルドカード(?)設定

    Apache2.2.3を利用しています。 virtualhost.confに50程度のサブドメインを、 下記のように設定をしています。 *************************************************** <VirtualHost *:80>   ServerName suzuki.example.com   DocumentRoot /var/www/html/suzuki/ </VirtualHost> <VirtualHost *:80>   ServerName sato.example.com   DocumentRoot /var/www/html/sato/ </VirtualHost> <VirtualHost *:80>   ServerName yamada.example.com   DocumentRoot /var/www/html/yamada/ </VirtualHost> ・ ・ ・ *************************************************** つまり *************************************************** <VirtualHost *:80>   ServerName ▲▲▲.example.com   DocumentRoot /var/www/html/▲▲▲/ </VirtualHost> *************************************************** のようなルールになっているのですが、 これをワイルドカード使ってできないでしょうか? 宜しくお願いします!

  • apacheのバーチャルホストの設定について

    XAMPPを一通りインストールして、apacheでバーチャルホストの設定を行っております。 環境:Windows2003server ApacheFriends XAMPP (Basispaket) version 1.7.1 + Apache 2.2.11 固定のグローバルIPアドレスは1つ持っており、仮に111.222.333.444としておきます。 バーチャルホストのドメイン名は2つ設定したいと思っており、 aaa.hogehoge.net bbb.hogehoge.net 他業者のDNSにて111.222.333.444をAレコードの値として、それぞれ割り当てしました。 ちなみに、hogehoge.netのAレコードは別のサーバーを当てています。 そこで、携帯から直接 http://aaa.hogehoge.netとhttp://bbb.hogehoge.net をアクセスしましたら、自社サーバーのC:/xampp/htdocs/にあるindex.htmが表示されました。 一応、名前解決は成功したとして、、、 念のため、Windowsのhostsに 111.222.333.444  aaa.hogehoge.net  bbb.hogehoge.net を追加しました。 apacheのメイン設定-httpd.conf ------------------------------ ServerRoot "C:/xampp/apache" Listen 80 ServerAdmin admin@localhost ServerName localhost:80 DocumentRoot "C:/xampp/htdocs/" ↓バーチャルホストのファイルをインクルード Include conf/extra/httpd-vhosts.conf ----以下、httpd-vhosts.confの中身 ホスト名を2つ設定 NameVirtualHost 111.222.333.444:80 <VirtualHost 111.222.333.444:80> ServerAdmin admin@aaa.hogehoge.net DocumentRoot C:/xampp/htdocs/aaa.hogehoge.net ServerName aaa.hogehoge.net ServerAlias www.aaa.hogehoge.net <Directory "C:/xampp/htdocs/aaa.hogehoge.net"> Options FollowSymLinks Includes ExecCGI AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost 111.222.333.444:80> ServerAdmin admin@bbb.hogehoge.net DocumentRoot C:/xampp/htdocs/bbb.hogehoge.net ServerName bbb.hogehoge.net ServerAlias www.bbb.hogehoge.net <Directory "C:/xampp/htdocs/bbb.hogehoge.net"> Options FollowSymLinks Includes ExecCGI AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> 設定後、apacheを再起動して、再度携帯電話からhttp://aaa.hogehoge.netとhttp://bbb.hogehoge.netにアクセスしたのですが、C:/xampp/htdocs/にあるindex.htmが表示されてしまい、C:/xampp/htdocs/aaa.hogehoge.net/index.htm、C:/xampp/htdocs/aaa.hogehoge.net/index.htmが表示されません。それぞれのパーミッション自体は766になっているので、こちらは違うと思うのですが、バーチャルホストの設定が悪いのか何処が悪いのか、何か設定が悪いのか全くわからなくなりました。参考書のマニュアルを見ると、「バーチャルホストを設定すると、メインホストの設定が無効になる」と書いてあったので、それでもメインのホストが表示されてしまうということは、バーチャルホストの設定に問題があるのだと自分なりに察知しております。 どなたか、考えられる原因や心当たりある事がございましたら、ご教授をお願いします。

  • バーチャルホストで、同じページをSSLとNon-SSLでアクセスできるようにしたい

    VPSでレンタルサーバをかりています。 (仮想のroot権限を持っています) で、バーチャルホストを以下のようにしています。 ====================================== NameVirtualHost xxx.xxx.xx.xx <VirtualHost xxx.xxx.xx.xx> <Directory /var/www/example.com/htdocs> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ServerAdmin webmaster@example.com DocumentRoot /var/www/example.com/htdocs ServerName example.com ErrorLog /var/log/example.com-error_log CustomLog /var/log/example.com-access_log common </VirtualHost> <VirtualHost xxx.xxx.xx.xx> <Directory /var/www/www.example.com/htdocs> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ServerAdmin webmaster@example.com DocumentRoot /var/www/www.example.com/htdocs ServerName www.example.com ErrorLog /var/log/www.example.com-error_log CustomLog /var/log/www.example.com-access_log common </VirtualHost> <VirtualHost xxx.xxx.xx.xx> <Directory /var/www/admin.example.com/htdocs> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ServerAdmin webmaster@example.com DocumentRoot /var/www/admin.example.com/htdocs ServerName admin.example.com ErrorLog /var/log/admin.example.com-error_log CustomLog /var/log/admin.example.com-access_log common </VirtualHost> ====================================== で、 http://example.com/fdsfdshhfdsh http://www.example.com/fdsfdshhfdsh http://admin,example.com/fdsfdshhfdsh にアクセスできるわけですが、 https://example.com/fdkjsjf のようにSSLでアクセスできるようにしたいと思っています。 で、 「DocumentRoot /var/www/example.com/htdocs」はSSLなしの場合と同様にしたいと思います。 (SSL,NON-SSLは、ファイルを共有) こうするには、どうすればいいのでしょうか? どなたか教えていただければ幸いです。