두 개의 VirtualHost 웹사이트가 Apache 2.4에서 함께 작동하지 않습니다.

두 개의 VirtualHost 웹사이트가 Apache 2.4에서 함께 작동하지 않습니다.

Ubuntu 18.04에서 Apache 2.4를 실행하고 있습니다.

브라우저 표시줄에 입력한 모든 주소가 다음으로 리디렉션되는 문제가 있습니다.하나구성한 두 웹사이트 중 하나(이 경우 로 리디렉션되어야 하는 주소를 입력할 때마다 other-site아래의 웹사이트 jekyll만 표시됩니다.)

예를 들어:

  • jekyll브라우저에 입력하면 예상대로 jekyll 웹사이트가 표시됩니다.
  • other-site브라우저에 입력지킬 웹사이트도 보여줘, 해당 이름을 수신하는 다른 VirtualHost가 있더라도

나는 이것들을 가지고 있다두 개의 별도 사이트VHost로 구성됨 /etc/apache2/sites-available(둘 다활성화됨을 통해 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

ServerName각 후속 줄이 이전 줄을 대체하므로 Apache 구성 파일의 속성을 반복할 필요가 없습니다 .

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와 넓게 열린 와일드카드를 사용하는 방법을 확인하세요 . 이는 정의된 구성 파일과 일치하지 않는 트래픽에 대해 포괄적으로 처리된다는 것을 의미합니다.

메모:Directory관련 없는 블록을 제거하고 각 호스트가 자체 오류 로그를 사용하도록 Apache 구성 파일이 간소화되었습니다 .

관련 정보