Apache-2.4

Apache vhost 返回兩個不同的站點

  • March 2, 2018

我在伺服器上配置了兩個子域(主域和暫存),並且對於暫存它返回兩個不同的路徑。有時它會返回正確的 ( /var/www/html_staging) 但有時(當您刷新 Web 瀏覽器幾次或轉到子頁面時)它會返回主網站 ( /var/www/html)

Ubuntu 16.04 上的 Apache 2.4

有 3 種虛擬主機配置,兩種用於主網站,一種用於登台。在埠 80 的 vhost 配置中有從 http 到 https 的重定向。

000-default.conf:

<VirtualHost *:80>
   ServerName website.example.com
   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
   ServerAdmin info@example.com
   DocumentRoot /var/www/html


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

預設-ssl.conf:

<IfModule mod_ssl.c>
   <VirtualHost *:443>
       ServerAdmin info@example.com
       DocumentRoot /var/www/html
       ServerName website.example.com

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined

       SSLEngine on
       SSLCipherSuite ALL:!EXP:!NULL:!ADH:!LOW
       SSLCertificateFile /etc/apache2/ssl/*.example.com.crt
       SSLCertificateKeyFile /etc/apache2/ssl/*.example.com.key
       SSLCACertificateFile /etc/apache2/ssl/bundle.crt

       <FilesMatch "\.(cgi|shtml|phtml|php)$">
               SSLOptions +StdEnvVars
       </FilesMatch>
       <Directory /usr/lib/cgi-bin>
               SSLOptions +StdEnvVars
       </Directory>
   </VirtualHost>
</IfModule>

staging.conf:

<VirtualHost *:80>
   ServerName websitestaging.example.com
   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
   ServerAdmin info@example.com
   DocumentRoot /var/www/html_staging

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


<IfModule mod_ssl.c>
   <VirtualHost _default_:443>
       ServerName websitestaging.example.com
       ServerAdmin info@example.com
       DocumentRoot /var/www/html_staging

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined

       SSLEngine on
       SSLCipherSuite ALL:!EXP:!NULL:!ADH:!LOW
       SSLCertificateFile /etc/apache2/ssl/*.example.com.crt
       SSLCertificateKeyFile /etc/apache2/ssl/*.example.com.key
       SSLCACertificateFile /etc/apache2/ssl/bundle.crt

   </VirtualHost>
</IfModule>

的輸出apache2ctl -S

VirtualHost configuration:
*:80                   is a NameVirtualHost
        default server website.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost website.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
        port 80 namevhost websitestaging.example.com (/etc/apache2/sites-enabled/staging.conf:1)
*:443                  is a NameVirtualHost
        default server website.example.com (/etc/apache2/sites-enabled/default-ssl.conf:2)
        port 443 namevhost website.example.com (/etc/apache2/sites-enabled/default-ssl.conf:2)
        port 443 namevhost websitestaging.example.com (/etc/apache2/sites-enabled/staging.conf:15)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

這種行為的原因是什麼?為什麼有時登台是從正確的路徑載入網站,有時是從主路徑載入?它正確載入的主網站,這只影響暫存。

以防萬一您遇到與上述相同的問題。只需重新啟動整個伺服器,而不僅僅是 apache。伺服器重新啟動後,一切正常。可能有某種記憶體或配置沒有被正確刪除,它正在影響整個伺服器。

引用自:https://serverfault.com/questions/898017