我的服务器信息是
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 19 2015 21:43:13
我正在尝试为 2 个不同的站点配置虚拟主机:biz.example.com 和 pin.example.com,它们托管在同一服务器上。 “var/www/html/”下有两个不同的文件夹,分别名为“biz”和“pin”,其中包含上述两个网站的受人尊敬的项目文件。我正在尝试按以下方式配置它。
在/etc/hosts下面的配置中
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
xxx.xxx.xxx.xxx biz.example.com
xxx.xxx.xxx.xxx pin.example.com
xxx.xxx.xxx.xxx 替换为服务器 IP 地址。
在 /etc/httpd/conf/httpd.conf 中
IncludeOptional sites-enabled/*.conf
现在,在 /etc/httpd/sites-available 下有 biz.conf 和 pin.conf 文件。我还在 /etc/httpd 下启用了站点启用文件夹,其中有 2 个文件使用以下命令指向站点可用文件夹的 biz.conf 和 pin.conf
ln -s /etc/httpd/sites-available/biz.conf /etc/httpd/sites-enabled/biz.conf
ln -s /etc/httpd/sites-available/pin.conf /etc/httpd/sites-enabled/pin.conf
biz.conf 有以下内容
<VirtualHost *:80>
ServerName http://biz.example.com/
ServerAlias http://biz.example.com/
DocumentRoot "/var/www/html/biz"
<directory "/var/www/html/biz">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from 127.0.0.1
</directory>
</VirtualHost>
pin.conf 文件中的配置被提到为
<VirtualHost *:80>
ServerName http://pin.example.com/
ServerAlias http://pin.example.com/
DocumentRoot "/var/www/html/pin"
<directory "/var/www/html/pin">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from 127.0.0.1
</directory>
</VirtualHost>
在此设置中,如果我尝试访问http://biz.example.com/,正在加载正确的网站(商业网站)。但如果我尝试访问http://pin.example.com/,然后还会加载商务网站而不是固定网站。多个配置无法协同工作。
我还尝试将 biz.conf 和 pin.conf 的虚拟配置合并到单个文件 biz.conf 中,但效果不佳。
答案1
回答:
1) 必须删除 ServerName 和 ServerAlias 中的尾部斜杠
2)这里,我们可以删除ServerAlias,并且ServerName和ServerAlias都是相同的。
答案2
从路径中删除双引号
DocumentRoot /var/www/html/pin
<directory /var/www/html/pin>
答案3
在此设置中,如果我尝试访问http://biz.example.com/,正在加载正确的网站(商业网站)。但如果我尝试访问http://pin.example.com/,然后还会加载商务网站而不是固定网站。
这是因为ServerName
和ServerAlias
指令不匹配(语法错误),在这种情况下,第一个定义的指令VirtualHost
获取所有请求。
文档中使用非常相似的配置描述了该行为:
在单个 IP 地址上运行多个基于名称的网站 (httpd.apache.org/docs/2.4/vhosts/examples.html)
# Ensure that Apache listens on port 80 Listen 80 <VirtualHost *:80> DocumentRoot "/www/example1" ServerName www.example.com # Other directives here </VirtualHost> <VirtualHost *:80> DocumentRoot "/www/example2" ServerName www.example.org # Other directives here </VirtualHost>
星号匹配所有地址,因此主服务器不处理任何请求。由于 的虚拟主机
ServerName www.example.com
在配置文件中排在第一位,因此它的优先级最高,可以看作是默认或者基本的服务器。这意味着,如果收到的请求与指定的指令之一不匹配ServerName
,则该请求将由该first 提供服务<VirtualHost>
。
解决方案:
必须
ServerName
没有http://
前缀且没有尾部斜杠,即ServerName biz.example.com
和
ServerName pin.example.com
ServerAlias
可以删除,因为它具有相同的值ServerName
<Directory>
并且</Directory>
应该以大写字母开头旧的 Apache 2.2 访问控制语法应更改为新的 Apache 2.4
Require
语法。Order Deny,Allow Allow from 127.0.0.1
应替换为
Require local
看