2 つの VirtualHost Web サイトは Apache 2.4 では連携して動作しません

2 つの VirtualHost Web サイトは Apache 2.4 では連携して動作しません

Ubuntu 18.04でApache 2.4を実行しています

ブラウザバーに入力したアドレスがすべてリダイレクトされるという問題が発生しています1つ私が設定した 2 つの Web サイトのうち (この場合、 にリダイレクトするアドレスを入力するたびにother-site、 の Web サイトのみが表示されますjekyll。)

例えば:

  • jekyllブラウザに入力すると、予想通りjekyllのウェブサイトが表示されます
  • other-siteブラウザで入力するjekyllのウェブサイトも表示されますその名前でリッスンしている別のVirtualHostが存在する場合でも

私はこれを持っています2つの別々のサイト/etc/apache2/sites-available(両方とも)VHostとして構成されている有効経由a2ensite

  • jekyll.conf

    <VirtualHost *:80>
          DirectoryIndex index.html
          DocumentRoot /var/www/jekyll/_site
          LimitRequestFieldSize 48000
           <Directory />
                  Options FollowSymLinks
                  AllowOverride None
          </Directory>
          <Directory /var/www/jekyll/_site>
                  AllowOverride All
                  Order allow,deny
                  allow from all
          </Directory>
          <Directory /var/www/jekyll/_site/>
                  Order allow,deny
                  allow from all
          </Directory>
    
          ServerName jekyll
          ServerName http://jekyll
          ServerName http://localhost/jekyll
    
          ServerAdmin webmaster@localhost
    </VirtualHost>
    
  • 他のサイト.conf

    <VirtualHost *:80>
          DirectoryIndex index.html
          DocumentRoot /var/www/other-site/_site
          LimitRequestFieldSize 48000
           <Directory />
                  Options FollowSymLinks
                  AllowOverride None
          </Directory>
          <Directory /var/www/other-site/_site>
                  AllowOverride All
                  Order allow,deny
                  allow from all
          </Directory>
          <Directory /var/www/other-site/_site/>
                  Order allow,deny
                  allow from all
          </Directory>
    
          ServerName other-site
          ServerName http://other-site
          ServerName http://localhost/other-site
    
          ServerAdmin webmaster@localhost
    
    </VirtualHost>
    

/etc/hostsまた、localhost が各サイトにリダイレクトされるように、次のエントリも追加しました。

127.0.0.1       localhost
127.0.0.1       localhost/jekyll
127.0.0.1       localhost/other-site
127.0.0.1       jekyll
127.0.0.1       other-site
127.0.1.1       felipe-Inspiron-7559

答え1

Apache 構成ファイル内の属性ServerNameは繰り返す必要はありません。後続の各行が前の行を置き換えるからです。

ServerName代わりに、次のように使用できますServerAlias:

ServerName jekyll
ServerAlias jekyll.local *.jekyll *.jekyll.local

これは非論理的であることに注意してください:

127.0.0.1       localhost/jekyll
127.0.0.1       localhost/other-site

これらはドメイン (またはサブドメイン) ではなく、 の下のパスですlocalhost。その結果、 のみlocalhostが観察されます。これが、上記のように Apache 構成にこれを含めなかった理由です。

これを念頭に置いて、三つApache 構成ファイル:

000-jekyll.conf

<VirtualHost *:80>
      ServerAdmin webmaster@localhost
      ServerName jekyll
      ServerAlias jekyll.local *.jekyll *.jekyll.local

      DirectoryIndex index.html
      DocumentRoot /var/www/jekyll/_site
      LimitRequestFieldSize 48000

      <Directory /var/www/jekyll/_site>
          Options FollowSymLinks
          AllowOverride All
          Order allow,deny
          Allow from all
      </Directory>

      ErrorLog ${APACHE_LOG_DIR}/jekyll-error.log
      CustomLog ${APACHE_LOG_DIR}/jekyll-access.log combined
</VirtualHost>

001-other.conf

<VirtualHost *:80>
      ServerAdmin webmaster@localhost
      ServerName other-site
      ServerAlias other-site.local *.other-site *.other-site.local

      DirectoryIndex index.html
      DocumentRoot /var/www/other-site/_site
      LimitRequestFieldSize 48000

      <Directory /var/www/other-site/_site>
          Options FollowSymLinks
          AllowOverride All
          Order allow,deny
          Allow from all
      </Directory>

      ErrorLog ${APACHE_LOG_DIR}/other-error.log
      CustomLog ${APACHE_LOG_DIR}/other-access.log combined
</VirtualHost>

999-default.conf

<VirtualHost *:80>
      ServerAdmin webmaster@localhost
      ServerName localhost
      ServerAlias *.localhost * *.*

      DirectoryIndex index.html
      DocumentRoot /var/www
      LimitRequestFieldSize 48000

      <Directory /var/www>
          Options FollowSymLinks
          AllowOverride All
          Order allow,deny
          Allow from all
      </Directory>

      ErrorLog ${APACHE_LOG_DIR}/local-error.log
      CustomLog ${APACHE_LOG_DIR}/local-access.log combined
</VirtualHost>

Apache は、設定ファイルの順序に基づいてトラフィックを処理します。したがって、 で指定されたドメインに一致するドメインは、000-jekyll.confそのファイルによって処理されます。一致するものが見つからない場合は、001-other.confがチェックされます。一致するものが見つからない場合は、999-default.confが使用されます。ファイルServerAlias内の999-default.confと、それがどのようにワイルドカードに依存しているかに注意してください。これは、定義された設定ファイルと一致しないトラフィックのキャッチオールとして扱われることを意味します。

注記:Apache 構成ファイルは、無関係なDirectoryブロックを削除し、各ホストが独自のエラー ログを使用するように合理化されました。

関連情報