Dois sites VirtualHost não funcionam juntos no Apache 2.4

Dois sites VirtualHost não funcionam juntos no Apache 2.4

Estou executando o Apache 2.4 no Ubuntu 18.04

Estou tendo esse problema onde todos os endereços que digito na barra do navegador são redirecionados paraumdos dois sites que configurei (neste caso, sempre que digito um endereço que deve redirecionar para other-site, é mostrado jekyllapenas o site abaixo).

Por exemplo:

  • digitar jekyllno navegador me mostra o site jekyll, como esperado
  • digitando other-siteno navegadortambém me mostra o site do jekyll, mesmo que haja outro VirtualHost escutando esse nome

eu tenho essesdois sites separadosconfigurado como VHosts em /etc/apache2/sites-available(amboshabilitadoatravés da 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>
    
  • outro-site.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>
    

Também adicionei essas entradas /etc/hostspara que o localhost seja redirecionado para cada site:

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

Responder1

O ServerNameatributo nos arquivos de configuração do Apache não precisa ser repetido, pois cada linha subsequente substituirá as anteriores.

Em vez disso, você pode usar ServerNameassim ServerAlias:

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

Observe que isso é ilógico:

127.0.0.1       localhost/jekyll
127.0.0.1       localhost/other-site

Estes não são domínios (ou subdomínios), mas caminhos sob localhost. Como resultado, apenas localhostserá observado. É por isso que não o incluí na configuração do Apache conforme mencionado acima.

Então, com isso em mente, você pode tertrêsArquivos de configuração do 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>

O Apache processa o tráfego com base na ordem dos arquivos de configuração. Portanto, qualquer domínio que corresponda aos especificados 000-jekyll.confserá tratado por esse arquivo. Se nenhuma correspondência for encontrada, 001-other.confserá verificado. Se nenhuma correspondência for encontrada, 999-default.confserá usado. Observe o ServerAliasarquivo 999-default.confe como ele depende de curingas abertos. Isso significa que ele será tratado como um recurso genérico para o tráfego que não corresponde aos arquivos de configuração definidos.

Observação:Os arquivos de configuração do Apache foram simplificados para eliminar Directoryblocos irrelevantes e para que cada host use seus próprios logs de erros.

informação relacionada