You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Meta Correio <me...@gmail.com> on 2016/02/13 12:43:13 UTC
[users@httpd] Virtual Host Setup Help
Hi,
I am conducting some setup tests and have come to a point where I could use
some help.
Tests are using Apache version 2.2.31 for Windows. OS is Windows 7 Ultimate
but I have seen the same exact behavior on Windows Server 2008 R2.
I have setup some virtual hosts and they don't seem to work as expected.
So, the first setup goes like this (only the relevant parts):
*Code:*
Listen 192.168.1.12:80
NameVirtualHost 192.168.1.12:80
<VirtualHost 192.168.1.12:80>
DocumentRoot "/Apache22/htdocs"
ServerName serverone.tld
ErrorLog "/Apache22/logs/serverone-error.log"
CustomLog "/Apache22/logs/serverone-access.log" combined
</VirtualHost>
<VirtualHost 192.168.1.12:80>
DocumentRoot "/Apache22/htdocs"
ServerName 192.168.1.12
ErrorLog "/Apache22/logs/192.168.1.12-error.log"
CustomLog "/Apache22/logs/192.168.1.12-access.log" combined
</VirtualHost>
Using my browser I have made the following requests:
http://serverone.tld
http://1 <http://127.0.0.1/>92.168.1.12
Both requests are logged under the first virtual host log file
(serverone.access.log).
I would expect that the requests would be logged on different files
(serverone-access.log and 192.168.1.12-access.log).
I have checked both requests and the Host: is different. On the first
request it's Host: serverone.tld and on the second request it's Host:
192.168.1.12
My log files is using the following format:
*Code:*
LogFormat "Host:%{Host}i|CServerName:%v| %h %l %u %t \"%r\" %>s %b
\"%{Referer}i\" \"%{User-Agent}i\"" combined
And here is the serverone-access.log content:
*Code:*
Host:serverone.tld|CServerName:serverone.tld| 192.168.1.12 - -
[13/Feb/2016:11:27:23 +0000] "GET / HTTP/1.1" 304 -
Host:192.168.1.12|CServerName:serverone.tld| 192.168.1.12 - -
[13/Feb/2016:11:28:22 +0000] "GET / HTTP/1.1" 304 -
So, Apache does see different request Host: for each one of the requests.
I have read the documentation available at
https://httpd.apache.org/docs/2.2/en/vhosts/name-based.html
and the important information seems to be the following:
*Quote:*
...when a request arrives, the server will first check if it is using an IP
address that matches the NameVirtualHost. If it is, then it will look at
each <VirtualHost> section with a matching IP address and try to find one
where the ServerName or ServerAlias matches the requested hostname. If it
finds one, then it uses the configuration for that server. If no matching
virtual host is found, then the first listed virtual host that matches the
IP address will be used.
So, the question is, why does Apache place both requests under the same
Virtual Host when, on a scenario using Virtual Hosts under the
NameVirtualHost paradigm it should look at the Host: to decide what Virtual
Host to use.
Any help is welcome.
...
Oh, and by the way, the following works flawlessly:
*Code:*
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Apache22/htdocs"
ServerName serverone.tld
ErrorLog "/Apache22/logs/serverone-error.log"
CustomLog "/Apache22/logs/serverone-access.log" combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Apache22/htdocs"
ServerName 192.168.1.12
ErrorLog "/Apache22/logs/192.168.1.12-error.log"
CustomLog "/Apache22/logs/192.168.1.12-access.log" combined
</VirtualHost>
The above, for the previously described requests, produce two distinct log
files:
serverone-access.log and 192.168.1.12-access.log
and as expected, they respectively contain:
*Code:*
Host:serverone.tld|CServerName:serverone.tld| 192.168.1.12 - -
[13/Feb/2016:11:37:48 +0000] "GET / HTTP/1.1" 304 -
and
*Code:*
Host:192.168.1.12|CServerName:192.168.1.12| 192.168.1.12 - -
[13/Feb/2016:11:37:38 +0000] "GET / HTTP/1.1" 304 -
Of course, on this last setup that works, I am telling Apache to ignore the
IPs and make decisions based exclusively on the ServerName.
Anyway, can anyone please let me know, considering the above
configurations, why the different behavior.
Thank you.
John
Fwd: FW: [users@httpd] Virtual Host Setup Help
Posted by Meta Correio <me...@gmail.com>.
Eric,
Thank you very much for your reply.
I have two requests:
*http://serverone.tld <http://serverone.tld>http://1
<http://127.0.0.1/>92.168.1.12*
On the first one the request header Host is: “*Host: serverone.tld*”
And on the second one the request header Host is: “*Host: 192.168.1.12*”
Now, I have run the *httpd.exe –S* command and the result is as follows:
*Code:*
VirtualHost configuration:
192.168.1.12:80 is a NameVirtualHost
default server serverone.tld (D:/Apache22/conf/httpd.conf:51)
port 80 namevhost serverone.tld (D:/Apache22/conf/httpd.conf:51)
port 80 namevhost 192.168.1.12 (D:/Apache22/conf/httpd.conf:65)
So, I have one NameVirtualHost, 192.168.1.12:80. This NVH has three
options: *default, serverone.tld and 192.168.1.12*.
The test that is failing is the one where “*Host: 192.168.1.12*”, so I will
focus on this one. It seems to me that the Apache server cannot match
the “*Host:
192.168.1.12*” with the ServerName (ServerName 192.168.1.12) and, because
of this unexpected mismatch, it’s using the *default* virtual host for
serving the request.
A simple test that confirms my suspicion involves changing the virtual host
definition order in the configuration file:
*Code:*
Listen 192.168.1.12:80
NameVirtualHost 192.168.1.12:80
<VirtualHost 192.168.1.12:80>
DocumentRoot "/Apache22/htdocs"
ServerName 192.168.1.12
ErrorLog "/Apache22/logs/192.168.1.12-error.log"
CustomLog "/Apache22/logs/192.168.1.12-access.log" combined
</VirtualHost>
<VirtualHost 192.168.1.12:80>
DocumentRoot "/Apache22/htdocs"
ServerName serverone.tld
ErrorLog "/Apache22/logs/serverone-error.log"
CustomLog "/Apache22/logs/serverone -access.log" combined
</VirtualHost>
Is this configuration, if one runs *httpd.exe –S*, the default server
changed (as expected).
*Code:*
VirtualHost configuration:
192.168.1.12:80 is a NameVirtualHost
default server 192.168.1.12 (D:/Apache22/conf/httpd.conf:60)
port 80 namevhost 192.168.1.12 (D:/Apache22/conf/httpd.conf:60)
port 80 namevhost serverone.tld (D:/Apache22/conf/httpd.conf:67)
With the above *default* virtual hots set to *192.168.1.12*. the *http://1
<http://127.0.0.1/>**92.168.1.12* request is logged under the “correct”
virtual host. Not because it’s matched. Just because the “correct” virtual
host for the request happens to be the *default*.
Now, let’s see if I understood your answer correctly. You mention that the
Host is compared to the address/hostname in the <virtualhost> itself. So, I
believe you are saying that when the host is “*Host: 192.168.1.12*” it is
compared to <VirtualHost 192.168.1.12:80> and that being a match with the
first defined virtual host, it stops searching other virtual hosts,
completely ignoring the ServerName field.
Is my reading your answer correctly?
Thank you.
John
*From:* Eric Covener [mailto:covener@gmail.com <co...@gmail.com>]
*Sent:* Saturday, February 13, 2016 3:57 PM
*To:* users@httpd.apache.org
*Subject:* Re: [users@httpd] Virtual Host Setup Help
On Sat, Feb 13, 2016 at 6:43 AM, Meta Correio <me...@gmail.com>
wrote:
So, the question is, why does Apache place both requests under the same
Virtual Host when, on a scenario using Virtual Hosts under the
NameVirtualHost paradigm it should look at the Host: to decide what Virtual
Host to use.
There is an under-ocumented, less used/understood matching step for NVH
where the Host is compared to the address/hostname in the <virtualhost>
itself. This is what messes with your test using the IP address.
--
Eric Covener
covener@gmail.com
Re: [users@httpd] Virtual Host Setup Help
Posted by Eric Covener <co...@gmail.com>.
On Sat, Feb 13, 2016 at 6:43 AM, Meta Correio <me...@gmail.com>
wrote:
> So, the question is, why does Apache place both requests under the same
> Virtual Host when, on a scenario using Virtual Hosts under the
> NameVirtualHost paradigm it should look at the Host: to decide what Virtual
> Host to use.
>
There is an under-ocumented, less used/understood matching step for NVH
where the Host is compared to the address/hostname in the <virtualhost>
itself. This is what messes with your test using the IP address.
--
Eric Covener
covener@gmail.com