你可以帮我解决这三个问题 .

1)我可以删除,我的意思是每个虚拟主机配置文件设置它是一个很好的做法 remove DocumentRoot and Directory directives from Apache main configuration file

2)我为每个虚拟主机配置了ServerName指令,我是否还应该在apache主配置文件上设置ServerName?如果是这样, should it be the same as the host machine meaning the hostname not the FQDN 因为我在该主机上有一些VirtualHosts?

3)apache如何选择为一个VirtualHost服务而不是另一个?在我当前的场景 when I try to access a virtual host when it is down (配置未加载) it is automatically redirected to the other VirtualHost DocumentRoot . Same happens 其中两个虚拟主机都已加载但我使用IP而不是虚拟主机名进行访问 .

主配置文件:

Listen [host machine IP]:80

ServerName [host machine IP]:80
<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www"

<Directory "/var/www/html/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
.
.
.

第一个VirtualHost:mydomain.com

<VirtualHost *:80>  
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot "/var/www/mydomain.com/html"
    ServerAdmin adm@rmydomain.net
    ErrorLog "/var/log/httpd/mydomain.com.log"
    CustomLog "/var/log/httpd/mydomain.com" combined

<Directory "/var/www/mydomain.com/html/">
    DirectoryIndex index.php index.html
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

第二个VirtualHost:mydomain ** .net **

<VirtualHost *:80>
    DocumentRoot "/var/www/mydomain.net/html"
    ServerName mydomain.net
    ServerAlias www.mydomain.net

    ServerAdmin adm@mydomain.net
    ErrorLog "/var/log/httpd/mydomain.net_error.log"
    CustomLog "/var/log/httpd/mydomain.net_access.log" combined

<Directory "/var/www/mydomain.net/html/">
    AuthType Basic
    AuthName "Protegido"
    AuthUserFile /etc/httpd/conf/htpasswd
    Require valid-user
    DirectoryIndex index.php index.html
    Options FollowSymLinks
    AllowOverride All

</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.net [OR]
RewriteCond %{SERVER_NAME} =mydomain.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

谢谢 .