Centos 7 の複数の仮想ホストが連携して動作しない

Centos 7 の複数の仮想ホストが連携して動作しない

私のサーバー情報は

Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 19 2015 21:43:13

同じサーバーでホストされている 2 つの異なるサイト、biz.example.com と pin.example.com の仮想ホストを設定しようとしています。 'var/www/html/' の下に 'biz' と 'pin' という名前の 2 つの異なるフォルダーがあり、それぞれに上記の 2 つの Web サイトのプロジェクト ファイルがあります。 以下の方法で設定しようとしています。

/etc/hosts内の以下の設定

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

xxx.xxx.xxx.xxx biz.example.com
xxx.xxx.xxx.xxx pin.example.com

xxx.xxx.xxx.xxx はサーバーの IP アドレスに置き換えられます。

/etc/httpd/conf/httpd.conf内

IncludeOptional sites-enabled/*.conf

現在、/etc/httpd/sites-available の下には biz.conf と pin.conf ファイルがあります。また、/etc/httpd の下に sites-enabled フォルダーがあり、以下のコマンドを使用して sites-available フォルダーの biz.conf と pin.conf を指す 2 つのファイルがあります。

ln -s /etc/httpd/sites-available/biz.conf /etc/httpd/sites-enabled/biz.conf

ln -s /etc/httpd/sites-available/pin.conf /etc/httpd/sites-enabled/pin.conf

biz.confには以下の内容があります

<VirtualHost *:80>
ServerName http://biz.example.com/
ServerAlias http://biz.example.com/
DocumentRoot "/var/www/html/biz"
<directory "/var/www/html/biz">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from 127.0.0.1
</directory>
</VirtualHost>

pin.confファイル内の設定は次のように記述されています。

<VirtualHost *:80>
ServerName http://pin.example.com/
ServerAlias http://pin.example.com/
DocumentRoot "/var/www/html/pin"
<directory "/var/www/html/pin">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from 127.0.0.1
</directory>
</VirtualHost>

この設定では、アクセスしようとするとhttp://biz.example.com/正しいウェブサイト(ビジネスウェブサイト)が読み込まれています。しかし、アクセスしようとするとhttp://pin.example.com/また、ピン留めウェブサイトの代わりにビジネスウェブサイトが読み込まれます。複数の構成が連携して動作していません。

また、biz.conf と pin.conf の仮想構成を 1 つのファイル biz.conf 内にマージしようとしましたが、うまくいきませんでした。

答え1

答え:

1) ServerNameとServerAliasから末尾のスラッシュを削除する必要があります

2) ここで、ServerAlias を削除できます。また、ServerName と ServerAlias は両方とも同じです。

答え2

パスから二重引用符を削除します

DocumentRoot /var/www/html/pin
<directory /var/www/html/pin>

答え3

この設定では、アクセスしようとするとhttp://biz.example.com/正しいウェブサイト(ビジネスウェブサイト)が読み込まれています。しかし、アクセスしようとするとhttp://pin.example.com/、その後、ピンの Web サイトの代わりにビジネス Web サイトが読み込まれます。

これは、 とServerNameの両方のServerAliasディレクティブが一致しない (構文が間違っている) ためであり、この場合、最初に定義されたものVirtualHostがすべてのリクエストを取得します。

この動作は、非常によく似た構成でドキュメントに記載されています。

単一の IP アドレスで複数の名前ベースの Web サイトを実行する (httpd.apache.org/docs/2.4/vhosts/examples.html)

# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
    DocumentRoot "/www/example1"
    ServerName www.example.com

    # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/example2"
    ServerName www.example.org

    # Other directives here
</VirtualHost>

ServerName www.example.comアスタリスクはすべてのアドレスに一致するため、メインサーバーはリクエストを処理しません。仮想ホストは構成ファイルの先頭にあるため、優先度が最も高く、デフォルトまたは主要なサーバー。つまり、指定されたServerNameディレクティブのいずれにも一致しないリクエストが受信された場合、そのリクエストは最初にこの によって処理されます<VirtualHost>


解決:

  1. 接頭辞や末尾のスラッシュは不要です。つまりServerNamehttp://

    ServerName biz.example.com
    

    そして

    ServerName pin.example.com
    
  2. ServerAlias同じ値なので削除できますServerName

  3. <Directory></Directory>大文字で始まる必要があります

  4. 古い Apache 2.2 アクセス制御構文は、新しい Apache 2.4Require構文に変更する必要があります。

    Order Deny,Allow
    Allow from 127.0.0.1
    

    を次のように置き換える。

    Require local
    

    見る


関連情報