我目前有一个专用服务器,有两个指向它的静态 IP,还有两个指向各自静态 IP 的域名。这两个域都有虚拟主机,并且这两个域都解析到正确的 Apache 目录。我们将它们称为域 1(通过 DNS 关联到 IP1)和域 2(通过 DNS 关联到 IP2)。主机名设置为完全限定域名 (www.domain1.com)
问题是,如果我尝试在浏览器上浏览任一 IP,它总是将我引导至域 1。我希望情况并非如此。我希望直接 IP 要么解析为错误,要么解析为其特定域。我做错了什么?
编辑:正如评论者所问,这里是两个虚拟主机文件。000-default.conf 是空的。
domain1.com.conf,请注意,这是一个 Symfony 安装,无需 www 即可运行:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias domain1.com
DocumentRoot /var/www/html/domain1/web
<Directory /var/www/html/domain1/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/html/domain1>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName domain1.com
ServerAlias domain1.com
SSLEngine on
SSLCertificateFile "/etc/ssl/certs/domain1_com.pem"
SSLCertificateKeyFile "/etc/ssl/private/domain1.key"
SSLCACertificateFile "/etc/ssl/certs/domain1_cert.pem"
DocumentRoot /var/www/html/domain1/web
<Directory /var/www/html/domain1/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/html/domain1>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
www.domain2.conf,这个还没有ssl证书,所以我只放了80端口的虚拟主机;
<VirtualHost *:80>
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /var/www/html/domain2
<Directory /var/www/html/domain2>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/html/domain2>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/domain2.log
CustomLog /var/log/apache2/domain2.log combined
</VirtualHost>
答案1
ServerName domain1.com
ServerAlias domain1.com
只有在添加其他主机名时才需要别名。您不需要为已经是 ServerName 的内容添加别名。
关于主要问题,您有两个虚拟主机在侦听任何 IP 地址。当请求进入时,Apache 将查看侦听该地址的虚拟主机(在您的情况下是两个,无论您输入的 IP 地址是什么),并尝试匹配服务器名称。如果不能,它将仅返回第一个虚拟主机。
你想要的是类似以下内容的 -
<VirtualHost ip.address.one:80>
... Website One ...
</VirtualHost>
<VirtualHost ip.address.two:80>
... Website Two ...
</VirtualHost>
SSL 也一样(但显然使用端口 443)
这样,当请求通过 IP 地址进入时,它只会匹配正确的虚拟主机。