Why does Apache2 serve a subdomain when given an IP?
March 14, 2009 12:41 AM   RSS feed for this thread Subscribe

I have a domain hosted on a VPS running CentOS5. Whenever I enter http://my-vps-ip in my browser, I get redirected to http://subdomain.mysite.com. My limited Google-fu is no match for this particular problem, so I'm hoping the folks at AskMeFi can tell me how I should setup Apache so that http://my-vps-ip goes to http://mysite.com and not the subdomain?

Extracts from the httpd.conf file:
ServerName mysite.com
...
UseCanonicalName Off
conf file defining the subdomain:

<virtualhost *:80> 
        ServerName      subdomain.mysite.com
        DocumentRoot    /var/www/subdomain
        ErrorLog /var/log/httpd/subdomain_mysite_com-error.log
        CustomLog /var/log/httpd/subdomain_mysite-access.log combined
    <directory> 
        Options Indexes FollowSymLinks
        AllowOverride AuthConfig FileInfo Indexes Limit
       Order allow,deny
       Allow from all
     </directory> 
< /virtualhost> 
Also, FWIW - currently the rDNS lookup on my VPS IP goes to ns1.mysite.com.

Many thanks!
posted by your mildly obsessive average geek to computers & internet (6 comments total)
If you have a name-based virtualhost listening on all IPs, as you do with <virtualhost *:80>, the first virtualhost stanza will be the default, handling any queries which don't match ServerNames from other virtualhost stanzas. Which is to say that what you think is your main server configuration in httpd.conf isn't getting used, except to set up default values, some of which are being overridden by the corresponding values in virtualhost stanzas.

If you want mysite.com to be the default, you'll probably have to set up a virtualhost stanza for it, too, and make sure it appears before the subdomain.mysite.com virtualhost stanza. If you're doing this with files in conf.d, numbering them is one way to accomplish that: 00_mysite.com.conf, 10_subdomain.mysite.com.conf, etc.

See the "Main host goes away" section at http://httpd.apache.org/docs/2.2/vhosts/name-based.html for more.
posted by hades at 2:19 AM on March 14


hades - I already have the following virtual host entries setup at the end of my primary httpd.conf:

#Virtual Host for main server
<VirtualHost *:80>
ServerName www.mysite.com
ServerAdmin webmaster@mysite.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/mysite_com-error.log
CustomLog /var/log/httpd/mysite-access.log combined
</VirtualHost>

#Virtual Host to handle non www requests
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin webmaster@mysite.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/mysite_com-error.log
CustomLog /var/log/httpd/mysite-access.log combined
</VirtualHost>
Am I correct in understanding that the entries in my virtual hosts conf file is overriding these entries? So if I were to move these to the top of my virtual hosts conf file - Apache should stop serving subdomain.mysite.com for the VPS IP?
posted by your mildly obsessive average geek at 2:42 AM on March 14


Yes, that's the normal setup.
posted by ArkhanJG at 9:39 AM on March 14


Also, you can use _default_ so that you don't have to pay attention to the order of your vhosts.
posted by rhizome at 10:25 AM on March 14


There's probably a line in the middle of your main httpd.conf which reads:
Include conf.d/*.conf
If your subdomain is being configured from a file in conf.d, it's actually coming before those stanzas at the end of httpd.conf, because of the include statement's position. Like rhizome said, you can use _default_, or you can put your main server configs in conf.d files lexically earlier than your subdomain config files.

Also, you don't need two stanzas for www.mysite.com and mysite.com; you can put
ServerAlias mysite.com
in the first stanza and get rid of the second.
posted by hades at 10:42 AM on March 14


I tried adding the _default_ option to the Virtual Host entry in the httpd.conf but I think the "Include conf.d/*.conf line" is still taking precedence.

In any case, I moved the Virtual Host entry for mysite.com to the same conf file as the sub-domain and added the _default_ option. After that, it started working!

hades - thanks for the tip on the ServerAlias.
posted by your mildly obsessive average geek at 2:51 AM on March 15


« Older How does the ski boot stiffnes...   |   Finding a warm place with moun... Newer »

You are not logged in, either login or create an account to post comments