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!

More VirtualHost Questions

Status
Not open for further replies.

Bobum

Programmer
Feb 11, 2005
6
US
Ok - Here's the scenario...
I have a domain It's documentroot is /var/
I want to have test1.example1.com as a server alias BUT I want it's document root to be /var/And I want to have test2.example1.com as a server alias but I want it's document root to be /var/
Note they are the same domain, just different aliases.

Now setting up a ServerAlias is easy enough (ServerAlais test1.example1.com test2.example1.com)
But getting their documentroot values to change isn't...

Can I set up each one of those aliases as it's own virtual host?

example...
Code:

Listen 0.0.0.0:80
NameVirtualHost *:80

<VirtualHost *:80>
ServerName DocumentRoot "/var/</VirtualHost>

<VirtualHost *:80>
ServerName test1.example1.com
DocumentRoot "/var/</VirtualHost>

<VirtualHost *:80>
ServerName test2.example1.com
DocumentRoot "/var/</VirtualHost *:80>


So far this configuration is not working for me as all of the requests are just going to the first default setup. Is there something I am missing - something in the rest of my httpd.conf I should be looking for? Is doing this with the same domain but different aliases even possible?
 
The configuration that you have posted looks correct. The only thing that I would change is:

Listen *:80
(but it might work the way that you already have it - I've never tried it like that)

The first Virtual Host will always be what is served if apache can't recognize the servername (for whatever reason). This sounds like the condition that you are seeing. To prove or disprove this, just change the order of your VirtualHosts.

If restarting apache as Wishdiak mentioned wasn't the problem, then check your apache logs for more clues.
 
Well changing the ServerName to ServerAlias didn't do anything different. All requests are still getting served from the default docroot.

Further up in the httpd.conf I have the following defualts set outside of the virtuals :

ServerName DocumentRoot "/var/
Would commenting these out FORCE Apache to look inside the virtuals or is commenting these out bad?
 
No, that won't make any difference. Make sure you check out faq65-1831. What do your logs show?
 
Ok - I commented out the default values for ServerName and DocumentRoot. I moved test1.example1.com virtual server to the top and changed all the ServerNames to ServerAlias and we are much closer.

test1 & test2 are gettign served out of their respective docroots as expected. BUT - the is not being recognized and is defaulting to test1 (since it is the first in the list)

So here is the current virtual server setup...

Code:

#ServerName #DocumentRoot "/var/
Listen 0.0.0.0:80
NameVirtualHost *:80

<VirtualHost *:80>
ServerAlias test1.example1.com
DocumentRoot "/var/</VirtualHost>

<VirtualHost *:80>
ServerAlias DocumentRoot "/var/</VirtualHost>

<VirtualHost *:80>
ServerAlias test2.example1.com
DocumentRoot "/var/</VirtualHost *:80>

We're almost there...
 
Bobum,

Try changing your VirtualHost containers from
<VirtualHost *:80>
</VirtualHost *:80>

to

<VirtualHost *>
</VirtualHost *>

I have no idea why this makes a difference, but that's how it is in my httpd.conf and it works.

Wishdiak
A+, Network+, Security+, MCSA 2003 certified
 
Bobum,

This is apache2 I assume. Instead of listening on a specific IP, you can just specify the port (80).

Also, all the end tags should be just the tag name and nothing else, so </VirtualHost *:80> should only be </VirtualHost>

You don't need quotes around the document root unless there are spaces in it, so you can just say: DocumentRoot /var/
And it should be ServerName inside each virtual host, not ServerAlias. Also, ServerName should be commented out in the defaults like you appear to have done. DocumentRoot does not need to be commented out in the defaults.

I'd also recommend adding "UseCanonicalName Off" to the default config.

So your config should include something resembling the following:

Code:
#ServerName [URL unfurl="true"]www.example1.com[/URL]
DocumentRoot "/var/[URL unfurl="true"]www/html"[/URL]
UseCanonicalName Off

Listen 80
NameVirtualHost *:80

<VirtualHost *:80>
   ServerName [URL unfurl="true"]www.example1.com[/URL]
   DocumentRoot /var/[URL unfurl="true"]www/html[/URL]
</VirtualHost>

<VirtualHost *:80>
   ServerName test1.example1.com
   DocumentRoot /var/[URL unfurl="true"]www/html/test1[/URL]
</VirtualHost>

<VirtualHost *:80>
   ServerName test2.example1.com
   DocumentRoot /var/[URL unfurl="true"]www/html/test2[/URL]
</VirtualHost>

Steve Kiehl
Web Page Designer - Nanovox Productions
Fantasy Artist - Zeadi
 
First off thanks for all your help - all of you.
Secondly - I feel like an idiot.

After doing some more in-depth checking, I found that there was a welcome.conf file in the conf.d directory that was redirecting all requests to directories without a valid index page, to a single "Apache Welcome" page. I was expecting to get directory listings for each docroot but instead was being redirected to this welcome page, thus it was giving me the illusion that no changes I made were being applied. Once I added an index page into each of the docroots everything appears to be running ok. The "welcome.conf" appears to be a RedHat Enterprise thing...I didn't even notice it at first.

Here is the final version of the working httpd.conf file...I did take some of your reccomendations. Good catch Kiehlster on the extra :80 in the closing VirtualHost tag.

Code:
#ServerName [URL unfurl="true"]www.example1.com[/URL]
#DocumentRoot "/var/[URL unfurl="true"]www/html"[/URL]
UseCanonicalName Off

Listen 0.0.0.0:80
NameVirtualHost *:80

<VirtualHost *:80>
   ServerName [URL unfurl="true"]www.example1.com[/URL]
   DocumentRoot "/var/[URL unfurl="true"]www/html"[/URL]
</VirtualHost>

<VirtualHost *:80>
   ServerName test1.example1.com
   DocumentRoot "/var/[URL unfurl="true"]www/html/test1"[/URL]
</VirtualHost>

<VirtualHost *:80>
   ServerName test2.example1.com
   DocumentRoot "/var/[URL unfurl="true"]www/html/test2"[/URL]
</VirtualHost>
 
That's odd that RH Enterprise would have a welcome.conf thing. I've never heard of that in any of my years working with Apache on RH and Fedora. Although, I haven't had the money to invest in their Enterprise editions, so that could be why I've never come across it before.
 
Here it is for future reference...I assumed it was a RHEL thing since that is the OS we are using on this particular box...

This is named welcome.conf and is located in the conf.d directory and is included by default with the Apache setup because fo the following line in httpd.conf

Include conf.d/*.conf

Here's the code...

Code:
# 
# This configuration file enables the default "Welcome"
# page if there is no default index page present for
# the root URL.  To disable the Welcome page, comment
# out all the lines below.
#
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /error/noindex.html
</LocationMatch>
[code]
 
Code:
#
# This configuration file enables the default "Welcome"
# page if there is no default index page present for
# the root URL.  To disable the Welcome page, comment
# out all the lines below.
#
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /error/noindex.html
</LocationMatch>

Sorry - dunno how to edit a message on this board...
 
Yes, my RHEL 3 box is set up the same. Good discussion and good problems solving by all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top