Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NameVirtualHost and VirtualHost with multiple domains

Status
Not open for further replies.

tsltsl

Technical User
Jan 17, 2002
24
SE
Hi,
I have 2 different domains which are located at mydomain.com's nameservers. I have them also in my own dns.
In the mydomain.com conf I have specified under web settings that both domains should point to my one and only static IP address which is the one I have on my linux server.
I have setup VirtualHosts in apache and it works fine.
But, since I have my first site in httpd.conf for "virtualhost1".domain1.com it doesn't matter if I browse to aaa.domain1.com, bbb.domain1.com, ccc.domain1.com...I still recieve the content of the first site. (aaa, bbb or ccc is not a virtualhost) If I use "virtualhost1".domain1.com, "virtualhost2".domain1.com and so on it works like it should.
The real problem comes with the second domain. If I browse to i.e aaa.domain2.com that is not setup as a virtualserver I recieve content from "virtualhost1".domain1.com instead of an error like I want it to be.
The "virtualhost1".domain2.com works fine.

Can I somehow control this in httpd.conf or is the problem the settings at mydomain.com?

I have been trying to find info about this with no luck so far.

TIA // tsltsl
 
It sounds like your are using just the domain name as the ServerName within the vhost container i.e. ServerName domain1.com. When a request comes in for domain1.com or any sub-domain of domain1.com, the vhost defined for domain1.com will be served. Note that is a sub-domain of domain1.com just as foo.domain1.com is. You will need to create a vhost container for each sub-domain using the ServerName directive. If apache can't find the vhost requested, it will use the first one defined as the default. If I'm not understanding your problem, can you post the section of httpd.conf that defines your virtual hosts? Just edit anything you don't want us to see first. :)
 
RhythmAce, thanks for replying.
I'm however not sure I understand your post fully and therefore I'm including parts from my httpd.conf
Maybe you can spot some problems there.
The single static IP address has been changed to "ip.add.re.ss"
(the names have been changed to protect the innocent ;o)
--------------------------
ServerName domain1.com:80
DocumentRoot "/var/
NameVirtualHost ip.add.re.ss

## virt1.domain1.com ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin webmaster@domain1.com
DocumentRoot /var/ ServerName virt1.domain1.com
ErrorLog logs/virt1.domain1.com-error_log
CustomLog logs/virt1.domain1.com-access_log common
</VirtualHost>

## virt2.domain1.com ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin user2@domain1.com
DocumentRoot /home/user2/html
ServerName virt2.domain1.com
ErrorLog logs/virt2.domain1.com-error_log
CustomLog logs/virt2.domain1.com-access_log common
</VirtualHost>

## virt3.domain1.com ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin user3@domain1.com
DocumentRoot /home/user3/html
ServerName virt3.domain1.com
ErrorLog logs/virt3.domain1.com-error_log
CustomLog logs/virt3.domain1.com-access_log common
</VirtualHost>

## virt4.domain1.com ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin user4@domain1.com
DocumentRoot /home/user4/html
ServerName virt4.domain1.com
ErrorLog logs/virt4.domain1.com-error_log
CustomLog logs/virt4.domain1.com-access_log common
</VirtualHost>

## virt5.domain1.com ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin user5@domain1.com
DocumentRoot /home/user5/html
ServerName virt5.domain1.com
ErrorLog logs/virt5.domain1.com-error_log
CustomLog logs/virt5.domain1.com-access_log common
</VirtualHost>

## virt1.domain2.net ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin webmaster@domain2.net
DocumentRoot /home/domain2/user1/html
ServerName virt1.domain2.net
ErrorLog logs/virt1.domain2.net-error_log
CustomLog logs/virt1.domain2.net-access_log common
</VirtualHost>

## domain2.net ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin user2@domain2.net
DocumentRoot /home/domain2/user2/html
ServerName domain2.net
ErrorLog logs/virt2.domain2.net-error_log
CustomLog logs/virt2.domain2.net-access_log common
</VirtualHost>
----------------------------
Thanks // tsl
 
Hi again,
can anyone see what could be wrong with the above settings for virtualhosts.
Apache should only respond to the virtualhosts specified, not serve the default site for anything else.
//tsl
 
Solved it! It was all in the documentation (sorry).

I added a vhost container at top of the Virtualhost section looking like this:

## *.domain1.com ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin webmaster@domain1.com
DocumentRoot /var/ ServerName *.domain1.com
ErrorLog logs/nomatch.domain1.com-error_log
CustomLog logs/nomatch.domain1.com-access_log common
</VirtualHost>

This allowed me to direct every request that is not matched in the virtualhost section for domain &quot;domain1.com&quot; to another DocumentRoot

Further I reconfigured the vhost container for domain2.net as follows:

## virt1.domain2.net ##
<VirtualHost ip.add.re.ss:80>
ServerAdmin webmaster@domain2.net
DocumentRoot /home/domain2/user1/html
ServerName virt1.domain2.net
ServerAlias domain2.net *.domain2.net
ErrorLog logs/virt1.domain2.net-error_log
CustomLog logs/virt1.domain2.net-access_log common
</VirtualHost>

The ServerAlias directive allows Apache to serve requests for domain2.net and &quot;whatever&quot;.domain2.net with contents from virt1.domain2.net

//tsl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top