VirtualHosts only looks at first instance

Hey all,

So I’ve done a bunch of google and forum searching on this, but I’m stuck! I’m just trying to do simple name-based virtual hosting (different domains/content, same IP).

What I’ve done is uncomment the “NameVirtualHost *:80” line in listen.conf. Then I have one file “domain.conf” in vhosts.d/ with:


<VirtualHost *:80>
ServerName www.domain1.com
DocumentRoot /srv/www/htdocs/domain1
<Directory "/srv/www/htdocs/domain1">
Options None
AllowOverride None
Order allow,deny
 Allow from all
</Directory>

</VirtualHost>

<VirtualHost *:80>
ServerName www.domain2.com
DocumentRoot /srv/www/htdocs/domain2
<Directory "/srv/www/htdocs/domain2">
Options None
AllowOverride None
Order allow,deny
 Allow from all
</Directory>

</VirtualHost>

Now, I actually have many domains, but these are the only 2 vhosts I set up. No matter what domain I go to (even if I just use my IP), it uses whichever <VirtualHost> I put first, in this case, “www.domain1.com”'s root of htdocs/domain1. If I switch that around, and put domain2 first, everything gets directed to htdocs/domain2.

I have tried:

  • using * instead of *:80
  • separate .conf files for each domain (it just uses the first alphabetically)
  • <myip>:80 and <myip>
  • other things I can’t think of

I just can’t figure this out. Ideas?

I assume you are using apache server right?

Sorry, yes.

I don’t have apache server installed right now, but I remember using virtual hosts which I set up using yast. So instead of editing config files manually, I would recommend doing your setup through yast. That would be more productive. If you can’t figure out how to do that, I might install apache and try to figure out how did I do it earlier.

Tried that, same outcome. Except it switches it all to * instead of *:80

Then I will try to install apache and see what can I do. Maybe I can answer your question in half an hour or so.

I did my virtual host configuration just fine. I far as I can see, yast only created too files inside vhosts.d called www.yasar1.com www.yasar2.com, their contents are:

www.yasar2.com

<VirtualHost *>
DocumentRoot /home/yasar/public_html/yasar1
ServerName www.yasar1.com
ServerAdmin yasar11732@gmail.com
<Directory /home/yasar/public_html/yasar1>
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

www.yasar2.conf

<VirtualHost *>
DocumentRoot /home/yasar/public_html/yasar2
ServerName www.yasar2.com
ServerAdmin yasar11732@gmail.com
<Directory /home/yasar/public_html/yasar2>
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

ImageShack - Best place for all of your image hosting and image sharing needs](ImageShack - Best place for all of your image hosting and image sharing needs)

Yes, that’s what mine does also. But for every domain, it points it to the doc root defined in the first (alphabetically) conf file.

I tried mine, and it works just fine. I edited earlier post and send printscreen there, maybe you can see there what you have been doing wrong.

Edit:

I figured out the problem. It’s something related to the headers not being passed on to my server, so it’s not getting the proper domain name :frowning: Unrelated to vhosts config

I can’t see anything weird on your setup, are you trying to access your virtual host from your server machine, or from some other machine. If you are trying to access your virtual machines from your own server, that might be causing problems. If you want, you can send your external ip with pm to me, and also your server names so I can check out if it works or not.

Edit after seeing your edit: Hmm that is nothing we can do about it than.

Yes, for virtual hosting to work, the client has to send the line Host: <virtual.domain> to the server. All web browsers do this when you put the virtual domain as part of the URL. If you were doing testing with telnet or whatever you have to add this Host: line.