-->

Configuring virtual hosts on apache2

2020-07-29 00:28发布

问题:

I'm switching from Dreamhost to Rackspace Cloud hosting, since the formers ability to handle a rails app left something to be desired. I've got a CentOS server all set up and I've got my rails app deployed and the domains resolve and everything is great. Now I want to set it up so if I go directly to the IP address I'll get the the normal apache directory instead of the rails app. I thought the virtual host could manage this, but now both of them go to rails page or the apache page, as the case may be

<VirtualHost *:80>
  ServerName 123.456.789.101
  DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:80>
  ServerName mywebsite.com
  ServerAlias www.mywebsite.com
  DocumentRoot /rails/myapp/current/public
  <Directory /rails/myapp/current/public>
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>

I'm not quite sure how to proceed, or if this is just impossible to begin with. Thoughts?

回答1:

Apache takes the very first virtualhost to be it's primary host for the provided IP, (or all IP's in the case of *). Therefore you should be able to just setup your main virtual host record, make sure its the first vhost record that get's loaded and it will load that virtualhost whenever an unbound IP or unbound address is encountered. Then specify a second virtual host with the specific domains/subdomains you want to point to your rails app.

Please note that if you split your virtual host records across multiple files (i.e. sites-available/sites-enabled folders). Apache loads them in alphabetical order, often on debian based systems you will see the 000-default or similer file. This one uses numbers at the start to ensure its always the first one loaded and takes on the role of primary vhost



标签: apache2