Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

With one IP, how can I set up two domains where:

  1. *.foo.com is name based for port 80 only.
  2. *.bar.com catches all requests both 80 and 443 that are made to the IP, or *.bar.com

Can I do this using the sites-enabled scheme?

Update to make this more clear, I think I may have overstated my requirements:

If http.header.host == ^.*?.foo.com:
   then serve('/var/www/foo');
else if tcp.port == 443:
   serve.ssl('/var/www/bar');
else 
   serve('/var/www/bar')
fi
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Sure, all that happens with sites-enabled is that Apache reads everything in that file in as part of the config. You can have multiple virtual hosts in a file. You can have all the virtual hosts in apache2.conf and have nothing in sites-enabled. You could put all your vhosts in conf.d. The only difference with sites-enabled is that the a2ensite command knows to symlink files to sites-available.

As for your question:

You can have:

NameVirtualHost *:80
NameVirtualHost *:443
<Virtualhost *:80>
   ServerName www.foo.com
   ServerAlias *.foo.com
   ...
</VirtualHost>

<Virtualhost *:80>
   ServerName www.bar.com
   ServerAlias *.bar.com
   ...
</VirtualHost>

<Virtualhost *:443>
   ServerName www.bar.com
   ServerAlias *.bar.com
   SSLEngine on
   ...
</VirtualHost>

<Virtualhost 12.34.56.78:80>
   ServerName www.bar.com
   ServerAlias *.bar.com
   ...
</VirtualHost>
<Virtualhost 12.34.56.78:443>
   ServerName www.bar.com
   ServerAlias *.bar.com
   SSLEngine on
   ...
</VirtualHost>

You will need to have a wildcard SSL certificate for *.bar.com, and you'll only be able to use one SSL certificate on the server, rather than one per IP address.

ServerName doesn't take wildcards, but ServerAlias does, which is why I've used both.

share|improve this answer
    
Thanks, does order matter if I put them in separate files in sites enabled? –  Kyle Brandt Aug 11 '09 at 17:01
    
Only which one is first. The first one is the one that's used when a hostname doesn't match ServerName or ServerAlias. –  David Pashley Aug 11 '09 at 17:03
    
So are they just loaded in alphabetic order? I have tried your config, but when I try putting foo.com into my hosts file, I am still getting directed to the DocumentRoot of www.bar.com even with the flat file .. :-/ –  Kyle Brandt Aug 11 '09 at 17:21
    
You can specify a DocumentRoot for each VirtualHost –  sebthebert Aug 11 '09 at 18:11
    
sebthebert: I did, the problem is that I'm not getting the right one ... –  Kyle Brandt Aug 11 '09 at 18:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.