Why is http: // localhost redirected to my default virtual host after installing virtual hosts in Apache?

This is probably a simple question, but I want to better understand how Apache works with virtual hosts. I configure virtual hosts because I work on several sites at once, and I do not want to use subdirectories. I used Apache's httpd.conf file quite a lot by default, and DocumentRoot pointed to something like "/ www". I uncommented the virtual hosts and added the following:

  NameVirtualHost *: 80

 <VirtualHost *: 80>
     ServerName site1.dev
     DocumentRoot / www / site1
 </VirtualHost>

 <VirtualHost *: 80>
     ServerName site2.dev
     DocumentRoot / www / site2
 </VirtualHost>

Now, when I go to http: // localhost , I get the default page for site1.

I am sure there is a reason why this makes sense, but I do not quite understand. I would think that only requests explicitly addressed to http: //site1.test will be redirected through this directive, and it will not be just by default. Can someone explain why it is becoming standard.

+6
apache
source share
4 answers

http://httpd.apache.org/docs/1.3/vhosts/name-based.html

(should be true for 2.x)

"If no matching virtual host is found, the first virtual host matching the IP address will be used.

As a result, the first virtual host specified is the default virtual host. A DocumentRoot from the primary server will never be used if the IP address matches the NameVirtualHost directive. If you want to have a special configuration for requests that do not correspond to a specific virtual host, simply install this configuration in a container and first list it in the configuration file.

+11
source

Answer 1 is correct and I would add with the name virtualhosts as the first entry essentially matches any virtualhost that is not named elsewhere

it should ONLY be used to catch unintentional malformed and broken traffic

those. machene with one ip is called john.domain.com, running on www.domain.com and www.domain2.com, since the actual web servers on ip www.xxx.yyy.zzz can have an optimal configuration, for example,

<VirtualHost *:80> DocumentRoot /var/webserver/static-sites/unknown/ # a directory readable by apache with only a robots.txt denying everything ServerName bogus ErrorDocument 404 "/errordocuments/unknown-name.html" #custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine# ErrorLog /var/log/httpd/unknown-error.log CustomLog /var/log/httpd/unknown-access.log combined </VirtualHost> <VirtualHost *:80> DocumentRoot /var/webserver/static-sites/unknown/ # a possibly different directory readable by apache with only a robots.txt denying everything ServerName www.xxx.yyy.zzz ServerAlias john.domain.com ErrorDocument 404 "/errordocuments/ip-name.html" ErrorDocument 403 "/errordocuments/ip-name.html" #custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/ ErrorLog /var/log/httpd/ip-error.log CustomLog /var/log/httpd/ip-access.log combined </VirtualHost> <VirtualHost *:80> ServerName domain.com RedirectPermanent / http://www.domain.com/ ErrorLog logs/www.domain.com-error.log CustomLog logs/www.domain.com-access.log combined </VirtualHost> <VirtualHost *:80> DocumentRoot /var/webserver/ftpusers/domain ServerName www.domain.com ServerPath /domain ErrorLog logs/www.domain.com-error.log CustomLog logs/www.domain.com-access.log combined </VirtualHost> <VirtualHost *:80> ServerName domain2.com RedirectPermanent / http://www.domain2.com/ ErrorLog logs/www.domain2.com-error.log CustomLog logs/www.domain2.com-access.log combined </VirtualHost> <VirtualHost *:80> DocumentRoot /var/webserver/ftpusers/domain2 ServerName www.domain2.com ServerPath /domain2 ErrorLog logs/www.domain2.com-error.log CustomLog logs/www.domain2.com-access.log combined </VirtualHost> 
+4
source

Confirmation that for Apache 2.x the first virtual host (with the same port number) will be used if the corresponding virtual host is not found.

http://httpd.apache.org/docs/2.2/vhosts/details.html

"If no matching vhost is found, the request is sent from the first vhost with the corresponding port number, which is in the list for the IP address to which the client is connected"

You can always add this code below, place it under NameVirtualHost *:80 so that your default root directory is set by default if no other virtual hosts are found.

 <VirtualHost *:80> ServerName localhost DocumentRoot /my/default/document/root </VirtualHost> 
+2
source

One way to do this:

  • In your VirtualHosts configuration, enter the name of the local site that you want to include, instead of using the template: <VirtualHost site1.dev:80> instead of <VirtualHost *:80>

  • Disable NameVirtualHost *:80 , which can be done by commenting it in the vhosts.conf file

  • Both aliases for loopback IP are mentioned in the / etc / hosts file: 127.0.0.1 localhost site1.dev

What is it. You should see that localhost goes to the standard DocumentRoot by default, and site1.dev goes to the site that you configured as a virtual host.

0
source

All Articles