在Apache的同一个ip上托管多个网站,如何设置哪个是默认的?

I have multiple VirtualHosts in Apache, all listening on port 80 with their ServerNames set to different web addresses. When I go directly to the ip address, Apache sends me to one of those websites as default. I want to change which virtualhost handles requests to the ip address. How would I do this?

<!-- This is currently the default (probably because it's first alphabetically) -->
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName mywebsite.com
    DocumentRoot /var/www/testingother
    ...
</VirtualHost>


<!-- I want this to be default -->
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName myotherwebsite.com
    DocumentRoot /var/www/testing
    ...
</VirtualHost>

From my own apache configuration file:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

Note that this DocumentRoot is outside a VirtualHost block. I would assume you have something similar but if not, you can set it up like that. That would be easier then messing around with the order in which they appear.

Default virtual host is the first one to be loaded.

  • When in single file httpd.conf or vhosts.conf, just change the order of vhosts.
  • When in vhost file based config (e.g. Debian uses that), it is a convention to prefix vhost files with a number. Default is usually 000-default.

From Apache documentation:

If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).