Virtualhost

Apache 服務於子域而不是域

  • December 27, 2020

我的伺服器上啟動並執行了兩個站點(nextcloud.example.com(Nextcloud Instance)和example.com(Grav Instance))。我在伺服器上添加了 Postfix 配置,現在我也可以發送郵件了。但是我把我的證書弄亂了,所以我重做了……

現在的主要問題是,總是當我試圖打開example.com它時,它總是呼叫example.com/index.php/login它來呈現登錄頁面,nextcloud.example.com但我期待我的 Grav 首頁。當我呼叫example.com/admin它時,它也解析為example.com/index.php/login.

我對這個主題很陌生,所以我希望這將是相關的數據:

$ apachectl configtest
Syntax OK

我確實在此處插入了我的 conf 文件,但 StackExchange 將我的文本區域標記為垃圾郵件。所以我刪除了它們。我很高興為您提供更多資訊,我只是不被允許…

編輯:

apache2ctl -S
VirtualHost configuration:
127.0.1.1:443          example.com (/etc/apache2/sites-enabled/example.com-le-ssl.conf:2)
127.0.1.1:80           example.com (/etc/apache2/sites-enabled/example.com.conf:1)
5.252.225.176:443      nextcloud.example.com (/etc/apache2/sites-enabled/nextcloud.example.com-le-ssl.conf:2)
5.252.225.176:80       nextcloud.example.com (/etc/apache2/sites-enabled/nextcloud.example.com.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default 
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
<VirtualHost nextcloud.example.com:443>
 DocumentRoot /var/www/nextcloud.example.com/htdocs/
 ServerName  nextcloud.example.com

 <Directory /var/www/nextcloud.example.com/htdocs/>
   Require all granted
   AllowOverride All
   Options FollowSymLinks MultiViews

   <IfModule mod_dav.c>
     Dav off
   </IfModule>
 </Directory>
RewriteEngine off
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.

#RewriteCond %{SERVER_NAME} =nextcloud.example.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost example.com:443>
 DocumentRoot /var/www/example.com/htdocs/
 ServerName  example.com

 <Directory /var/www/example.com/htdocs/>
   Require all granted
   AllowOverride All
   Options FollowSymLinks MultiViews

   <IfModule mod_dav.c>
     Dav off
   </IfModule>
 </Directory>
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.

#RewriteCond %{SERVER_NAME} =example.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

您可能需要將 VirtualHost 指令中的主機名更改為*. 也就是說,而不是

<VirtualHost nextcloud.example.com:443>
 ServerName  nextcloud.example.com
 ...
</VirtualHost>
<Virtualhost example.com:443>
 ServerName  example.com
 ...
</VirtualHost>

嘗試

<VirtualHost *:443>
 ServerName  nextcloud.example.com
 ...
</VirtualHost>
<Virtualhost *:443>
 ServerName  example.com
 ...
</VirtualHost>

一般 VirtualHost 的參數應該是一個 IP 地址,或者*匹配所有的 IP 地址。

如果 Apache 接收連接的 IP 地址與 example.com 的 DNS 地址不同 - 例如,如果 example.com 在代理後面,則上述第一種形式將失敗。在這種情況下,Apache 將回退到從預設虛擬主機提供服務,這是第一個虛擬主機,在您的情況下似乎是 nextcloud.example.com。

請參閱VirtualHost基於名稱的虛擬主機

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