LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-02-2011, 01:53 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,849
Blog Entries: 31

Rep: Reputation: 180Reputation: 180
In Apache2, is hostname (from HTTP Host: header) parsed?


When Firefox makes a web site request, the HTTP header Host: provides the hostname (or if an IP address is given, it provides the IP address), plus a port number if the port number is not 80 (even if 80 is explicitly given in the URL, it is removed by Firefox). What I cannot tell from the Apache documentation online (for version 2.2) is whether or not the port number received in the request is stripped off before it is matched to a ServerName or ServerAlias directive. One concern (of many) is that there may be a browser which does not strip off the ":80" when the URL includes the ":80" (or ":443" in the case of HTTPS).

For this purpose, I have Apache configured with NameVirtualHost for every IP address and port it is listening on (with a Listen directive). So this is a question about NameVIrtualHost logic (if it's different in other cases). Should ServerName and/or ServerAlias include the port number? Or should I include both directives to provide a way to match both ways?
 
Old 11-03-2011, 07:13 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
no, it shouldn't. if you are defining a virtualhost on :80 and :443 then as they are on different ports they don't conflict. The port number is not passed as part of the hostname, just look at the headers you're sending, and there should be no port number in the Host header

Last edited by acid_kewpie; 11-03-2011 at 07:15 AM.
 
Old 11-03-2011, 01:22 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,849

Original Poster
Blog Entries: 31

Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by acid_kewpie View Post
no, it shouldn't. if you are defining a virtualhost on :80 and :443 then as they are on different ports they don't conflict. The port number is not passed as part of the hostname, just look at the headers you're sending, and there should be no port number in the Host header
It depends on the browser, and even version numbers in the case of Apache:
Code:
GET / HTTP/1.1
Host: faraday.ipal.net:80
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Code:
GET / HTTP/1.1
Host: faraday.ipal.net
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.10 (maverick) Firefox/3.6.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
In cases where it is NOT port 80, but a port specified in the URL, then the port number is included in the Host: header in cases where it does not for 80, unless it was specified as 80. When explicitly specified as 80, it seems to be acted on the same way as not specified. But over the limited browser tests I have done (firefox and lynx) I cannot be sure that this doesn't vary for others. Nevertheless, for some other port number, like 88 or 8080 or whatever, it does supply the port number in Host: more often (though in theory it doesn't need to, since it made a connection to the proper port at the TCP layer which can still be used to distinguish the accesses).

It seems as though Apache is parsing the host name and port number apart, and doing EITHER:

1. Comparing only the host name part to select the VirtualHost section.

2. Substituting the connected port number where missing from Host: and the Listen port number where missing from ServerName/ServerAlias, and comparing both host name and port number to select the VirtualHost section.

Perhaps both choices will always end up being the same where there is no specific conflict like (this example of a conflict):
Code:
<VirtualHost example.com:80>
    ServerName example.com:96
...
Then I have a case where I code the IP address (IPv6) in the URL:
Code:
GET /used/IP/address/instead/of/host/name HTTP/1.1
Host: [fcca::1a]
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.10 (maverick) Firefox/3.6.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
How do I match that? Since a port number can be used there, I assume I need the [] to separate the optional port number from the IPv6 address. But, Apache2 doesn't like the [] syntax. Apache1 didn't do IPv6 that I'm aware of (I didn't have IPv6 when I did have Apache1) but it did do IPv4 addresses and I was able to make separate web sites for the three cases:
1. No Host: header provided at all
2. Used the standard dotted-decimal IP address form as host
3. Used the decimal 32-bit number form as host
Perhaps Apache2 has deprecated one or both "address as host" forms. I just haven't found any documentation on the httpd.apache.org site that says that, so I cannot be sure. Nevertheless, sites with IP addresses as host names do exist on the internet.

FYI, I'm writing code to generate Apache configurations from a database of what sites are supposed to be present.
 
Old 11-03-2011, 06:58 PM   #4
d3vrandom
Member
 
Registered: Jun 2006
Location: Karachi, Pakistan
Distribution: OpenSUSE, CentOS, Debian
Posts: 59

Rep: Reputation: 9
You use the ip in the virtual host section and you can match multiple ips or different ports. This is how it's supposed to be.
Code:
namevirtualhost ipaddress:80
namevirtualhost ipaddress:81

<VirtualHost ipaddress:80 ip.address:81>
    ServerName example.com
</virtualhost>
Now if you want to match only the ip then you aren't doing name based virtual hosting. You are just doing ip based hosting in which case you omit the namevirtualhost line and just add the ip:
Code:
<virtualhost ipaddres:80>

</virtualhost>
Servername is not required.
 
Old 11-03-2011, 09:25 PM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,849

Original Poster
Blog Entries: 31

Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by d3vrandom View Post
You use the ip in the virtual host section and you can match multiple ips or different ports. This is how it's supposed to be.
Code:
namevirtualhost ipaddress:80
namevirtualhost ipaddress:81

<VirtualHost ipaddress:80 ip.address:81>
    ServerName example.com
</virtualhost>
Now if you want to match only the ip then you aren't doing name based virtual hosting. You are just doing ip based hosting in which case you omit the namevirtualhost line and just add the ip:
Code:
<virtualhost ipaddres:80>

</virtualhost>
Servername is not required.
How does Apache handle the case (for the purpose of match to select the VirtualHost section) when the Host: header has:

Code:
1.  an IPv4 address in dotted decimal form
2.  an IPv4 address in dotted decimal form with a port number
3.  an IPv4 address in integer decimal form
4.  an IPv4 address in integer decimal form with a port number
5.  an IPv4 address in integer hexadecimal form
6.  an IPv4 address in integer hexadecimal form with a port number
7.  an IPv6 address in bracketed hexadecimal form
8.  an IPv6 address in bracketed hexadecimal form with a port number
9.  an IPv6 address in non-bracketed hexadecimal form
Examples:

Code:
1.  Host: 192.168.1.20
2.  Host: 192.168.1.20:45
3.  Host: 3232235796
4.  Host: 3232235796:45
5.  Host: 0xc0a80114
6.  Host: 0xc0a80114:45
7.  Host: [fc00::114]
8.  Host: [fc00::114]:45
9.  Host: fc00::114
for URLs like:

Code:
1.  http://192.168.1.20/foo
2.  http://192.168.1.20:45/foo
3.  http://3232235796/foo
4.  http://3232235796:45/foo
5.  http://0xc0a80114/foo
6.  http://0xc0a80114:45/foo
7.  http://[fc00::114]/foo
8.  http://[fc00::114]:45/foo
9.  http://fc00::114/foo
At the very least it seems to be inconsistent. It is definitely not documented.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache2 - virtual host defaulting to default host slimjim Linux - Server 1 10-31-2009 02:47 AM
apache2 .html pages not being parsed mgichoga Debian 1 03-03-2007 09:58 PM
Non-Parsed-Header scripts soulsniper Linux - Software 0 11-22-2003 03:33 PM
Apache2, php code parsed as *.shtml bmar Linux - General 4 06-29-2003 05:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration