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.

I'm unsure of how to ask or describe in words what I'm trying to do, or if it can be done this way, so I decided to draw a diagram. Hopefully its fairly self explanatory:

Diagram of what im trying to do

I know I can do Named based virtual hosts all on Port 80, but can I place them on different ports and have the server work out what named host is which and which port they are on?


<VirtualHost _default_:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/foo-1.com
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/foo-1.com/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<VirtualHost _default_:80>
    DocumentRoot /var/www/foo-2.com
    ServerName foo-2.com
    <Directory "/var/www/foo-2.com">
        allow from all
        Options -Indexes
    </Directory>
</VirtualHost>
share|improve this question
    
Why don't you want to use standard vhosts ? –  Iain Aug 14 '12 at 20:25
    
Why are the VHosts on port 8080 and 8081, respectively? What is port 80 doing, then? Is there a proxy in here somewhere, or is Apache acting as one? –  BMDan Aug 14 '12 at 20:30
    
For some reason if i create to vhost on 80 with different names only one works correct, meaning one will work typing foo-1.com or www.foo-1.com but the other vhost will ony work typing "foo-2.com" but typing www.foo-2.com will redirect to foo-1.com. –  Kyle C. Aug 14 '12 at 20:31
    
I only used those ports as examples I was thinking of using apache as a proxy but was unsure of what or how to go about this or where to look. –  Kyle C. Aug 14 '12 at 20:33
    
So, rather than solve what is quite possibly an easy problem you invent some weird scheme ... Show is your vhost configuration –  Iain Aug 14 '12 at 20:39
show 3 more comments

2 Answers 2

up vote 3 down vote accepted

To have two name based virtual hosts working on the same IP address try something like this

NameVirtualHost *:80

<VirtualHost _*:80>
    ServerAdmin webmaster@localhost
    ServerName foo-1.com
    ServerAlias www.foo1.com
    DocumentRoot /var/www/foo-1.com
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/foo-1.com/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/foo-2.com
    ServerName foo-2.com
    ServerAlias www.foo-2.com
    <Directory "/var/www/foo-2.com">
        allow from all
        Options -Indexes
    </Directory>
</VirtualHost>

Note that each vhost has a ServerName and ServerAlias

share|improve this answer
    
Thank you much that worked great! appreciate all the help. –  Kyle C. Aug 14 '12 at 21:25
    
This is the better way to go rather than my other suggestion of using rewrites. What you ask is possible, but not a standard approach. –  Tim Koscielski Aug 14 '12 at 21:28
    
I have my server using the method described above as im not trying to do make things more complex than need be or reinvent the wheel or anything. Ive tryed other solutions similar to problem made in the comments above with no avail so thought maybe i needed to go about this a different way. However im still somewhat new to apache and the configuration part. And as always i try to do as much research and testing as possible before asking anything and I'll try to ask questions more specific to the actual problem in the future. But I do appreciate all the help from the community. Thanks again! –  Kyle C. Aug 14 '12 at 21:46
add comment

On first reading your scenario, my first response was why would you want to do it this way much like the others, but with Apache or PHP something like this is possible. You would be much better off getting virtual hosts to work properly on port 80 as this is what web site visitors expect, but this is my opinion.

To do what you'd like to accomplish, there are two options and you have to be comfortable that an HTTP redirect has to happen. No way around that one.

Your first option is to use PHP for this with something as follows...

  1. Your main Apache site would cover all default site requests using PHP
  2. PHP would read the headers, look at the URL and pass back a redirect to the browser
  3. The browser would then redirect to the correct URL

Apache supports a rewrite option as well and you could organize this for each site in the following manner

# First host rewrite
RewriteCond %{HTTP_HOST} ^www.\foo-1\.com$ [NC]
RewriteRule ^(.*)$ http://www.foo-1.com:8080/$1 [L,R=301]

# Second host rewrite
RewriteCond %{HTTP_HOST} ^www.\foo-2\.com$ [NC]
RewriteRule ^(.*)$ http://www.foo-2.com:8081/$1 [L,R=301]

However, if you are going to go to this level with rewrite conditions, it may be easier to figure out VHOSTS in Apache and get it working properly on your server.

share|improve this answer
    
Ok so it can be done then and that is really what I was after. If only out of pure curiosity if something like that could be done. I have several domain names and only one server and one ip using noip as my dns. and may not always want all servers running on port 80 for any number of various reasons. –  Kyle C. Aug 14 '12 at 21:29
    
Almost forgot, Thank you! :) –  Kyle C. Aug 14 '12 at 21:37
add comment

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.