Dos sitios web de VirtualHost no funcionan juntos en Apache 2.4

Dos sitios web de VirtualHost no funcionan juntos en Apache 2.4

Estoy ejecutando Apache 2.4 en Ubuntu 18.04

Tengo este problema donde todas las direcciones que escribo en la barra del navegador son redirigidas aunode los dos sitios web que he configurado (en este caso, cada vez que escribo una dirección a la que debo redirigir other-site, solo se muestra el sitio web debajo jekyll).

Por ejemplo:

  • Al escribir jekyllen el navegador me aparece el sitio web de Jekyll, como se esperaba.
  • escribiendo other-siteen el navegadorTambién me muestra el sitio web de Jekyll., aunque hay otro VirtualHost escuchando con ese nombre

Tengo estosdos sitios separadosconfigurado como VHosts en /etc/apache2/sites-available(ambosactivadoa través de 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>
    
  • otro-sitio.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>
    

También agregué estas entradas /etc/hostspara que localhost sea redirigido a cada sitio:

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

Respuesta1

No es necesario repetir el ServerNameatributo en sus archivos de configuración de Apache, ya que cada línea posterior reemplazará a las anteriores.

En su lugar, puedes usar ServerNamecon ServerAliasasí:

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

Tenga en cuenta que esto es ilógico:

127.0.0.1       localhost/jekyll
127.0.0.1       localhost/other-site

Estos no son dominios (o subdominios), sino rutas bajo localhost. Como resultado, sólo localhostse observará. Es por eso que no lo incluí en la configuración de Apache como se indicó anteriormente.

Entonces, con esto en mente, puedes tenertresArchivos de configuración de 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 procesa el tráfico según el orden de los archivos de configuración. Por lo tanto, cualquier dominio que coincida con los especificados 000-jekyll.confserá manejado por ese archivo. Si no se encuentran coincidencias, 001-other.confse comprobará. Si no se encuentran coincidencias, 999-default.confse utilizará. Tenga en cuenta lo que hay ServerAliasen el 999-default.confarchivo y cómo se basa en comodines abiertos. Esto significa que será tratado como un todo para el tráfico que no coincida con los archivos de configuración definidos.

Nota:Los archivos de configuración de Apache se simplificaron para eliminar Directorybloques irrelevantes y hacer que cada host use sus propios registros de errores.

información relacionada