我使用的是具有规范名称的服务器(在/etc/hostname和反向DNS中定义) server.example.com
root @ server:〜#uname -a
Linux server.example.com 3.16.0-4-amd64#1 SMP Debian 3.16.7-ckt11-1(2015-05-24)x86_64 GNU / LinuxApache / 2.4.10(Debian)服务器在server.example.com端口80
现在我安装了apache2并做了一些基本的configuration。 然后,我将VirtualHosts添加到/etc/apache2/sites-available并使用sites-enabled了sites-enabled符号链接。 除了ServerName server.example.com VirtualHost以外,一切正常。 当我尝试在资源pipe理器中访问该URI时,我得到默认虚拟主机的内容(没有指定ServerName )。 如果我把server.example.com.conf的ServerName改成不同的东西(比如test.example.com )并重新加载apache的configuration,那么我可以直接访问这个URI。 我想知道什么是错误的VirtualHost的ServerName相同的机器的规范名称。
而且, server.example.com.conf有DocumentRoot /var/www/subs/server ,其中包含index.html和munin文件夹。 默认的VirtualHost有DocumentRoot /var/www/html ,里面没有munin文件夹。 但是,当我尝试访问http://server.example.com/munin我可以进入/var/www/subs/server/munin ,而http://server.example.com/index.html将我/var/www/html/index.html 。
最后,当我把默认的VirtualHost的Redirect 404 / ,当我试图访问http://server.example.com/和http://server.example.com/munin时,我会得到404
增加了VirtualHosts的configuration
/etc/apache2/sites-available/forum.example.com.conf :
# Working subdomain configuration <VirtualHost *:80> ServerName forum.example.com ServerAdmin [email protected] DocumentRoot /var/www/subs/forum ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
/etc/apache2/sites-available/server.example.com.conf :
# Not working server.example.com configuration <VirtualHost *:80> ServerName server.example.com ServerAdmin [email protected] DocumentRoot /var/www/subs/server ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
/etc/apache2/sites-available/default.conf :
# Default VirtualHost configuration <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/html #Redirect 404 / ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
根@服务器:〜#apache2ctl -t
语法OK
增加 root@server:~# apache2 -S ouptut root@server:~# apache2 -S
[Tue Aug 11 11:07:32.705684 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOCK_DIR} is not defined [Tue Aug 11 11:07:32.706119 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_PID_FILE} is not defined [Tue Aug 11 11:07:32.706250 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_RUN_USER} is not defined [Tue Aug 11 11:07:32.706406 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_RUN_GROUP} is not defined [Tue Aug 11 11:07:32.706566 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOG_DIR} is not defined (... repeating few more times) [Tue Aug 11 11:07:32.744292 2015] [core:warn] [pid 20215] AH00111: Config variable ${APACHE_LOG_DIR} is not defined AH00526: Syntax error on line 74 of /etc/apache2/apache2.conf: Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}
而,
/etc/apache2/apache2.conf:74:Mutex file:${APACHE_LOCK_DIR} default /etc/apache2/envvars:21:export APACHE_LOCK_DIR=/var/lock/apache2$SUFFIX
在运行/etc/apache2/envvar我通过执行apache2 -S得到这个
VirtualHost configuration: *:80 is a NameVirtualHost default server server.example.com (/etc/apache2/sites-enabled/default.conf:1) port 80 namevhost server.example.com (/etc/apache2/sites-enabled/default.conf:1) port 80 namevhost download.example.com (/etc/apache2/sites-enabled/download.example.com.conf:1) port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1) alias www.example.com port 80 namevhost forum.example.com (/etc/apache2/sites-enabled/forum.example.com.conf:1) port 80 namevhost forumold.example.com (/etc/apache2/sites-enabled/forumold.example.com.conf:1) port 80 namevhost forumtest.example.com (/etc/apache2/sites-enabled/forumtest.example.com.conf:1) port 80 namevhost mail.example.com (/etc/apache2/sites-enabled/mail.example.com.conf:1) port 80 namevhost ro.example.com (/etc/apache2/sites-enabled/ro.example.com.conf:1) port 80 namevhost server.example.com (/etc/apache2/sites-enabled/server.example.com.conf:1) port 80 namevhost anotherexample.com (/etc/apache2/sites-enabled/anotherexample.com.conf:1) alias www.anotherexample.com ServerRoot: "/etc/apache2" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/var/log/apache2/error.log" Mutex watchdog-callback: using_defaults Mutex rewrite-map: using_defaults Mutex default: dir="/var/lock/apache2" mechanism=fcntl Mutex mpm-accept: 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
在基于debian的发行版上,默认的virtualhost根本不指定ServerName,这实际上意味着它以系统主机名作为其ServerName。
由于它是首先列出的,它将会用相同的ServerName来掩盖任何后来的虚拟主机。
我build议禁用发货的默认虚拟主机,并添加自己的说明一个服务器名称,但不使用您感兴趣的ServerName。
从apache-doc:如果没有指定ServerName,则服务器尝试通过对IP地址执行反向查找来推断主机名。 如果ServerName中没有指定端口,则服务器将使用传入请求中的端口。 为了获得最佳的可靠性和可预测性,您应该使用ServerName指令指定一个明确的主机名和端口。
+
本质上,主服务器被视为构build每个虚拟主机的“默认”或“基础”。 但是这些主要服务器定义在configuration文件中的定位在很大程度上是不相关的 – 当最后的合并发生时,主服务器的整个configuration已经被parsing。 因此,即使主服务器定义出现在虚拟主机定义之后,也可能会影响虚拟主机定义。