NameVirtualHost does not work: only loads default host

apache-2.2httpd.confnamevirtualhostvirtualhost

I have configured Apache 2.2 so that I can have 2 hosts. The problem is when I connect using the second host, the default one is loaded.

This is the vhosts.conf file:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin support@desytec.com
    DocumentRoot /var/www/proyectos/mutual/contratos/web
    ServerName contratos.mutual.dev
    ErrorLog /etc/httpd/logs/mutual-contratos-error_log
    <Directory "/var/www/proyectos/mutual/contratos/web">
       Options FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin support@desytec.com
    DocumentRoot /var/www/proyectos/spensiones/html
    ServerName biotempo.spensiones.dev
    ErrorLog /etc/httpd/logs/biotempo.spensiones.dev-error_log
    <Directory "/var/www/proyectos/spensiones/html">
       Options FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
    </Directory>
</VirtualHost>

And this is the "httpd -S" command output:

[root@orahost conf.d]# httpd -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
_default_:443          localhost.localdomain (/etc/httpd/conf.d/ssl.conf:81)
*:80                   is a NameVirtualHost
         default server contratos.mutual.dev (/etc/httpd/conf.d/vhosts.conf:3)
         port 80 namevhost contratos.mutual.dev (/etc/httpd/conf.d/vhosts.conf:3)
         port 80 namevhost biotempo.spensiones.dev (/etc/httpd/conf.d/vhosts.conf:16)
Syntax OK
[root@orahost conf.d]# 

To test, from a remote computer I try to load biotempo.spensiones.dev but the default server is displayed.

Any help, please?

Best Answer

You can give a try with below. I had solved the problem by using below in httpd.conf.

NameVirtualHost Server_IP_Address:80

<VirtualHost Server_IP_Address:80>

........

</VirtualHost>

Let us know if you are still facing issues.

EDIT: NOTE

The asterisks * match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName contratos.mutual.dev is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first .

The above configuration is what you will want to use in almost all name-based virtual hosting situations. The only thing that this configuration will not work for, in fact, is when you are serving different content based on differing IP addresses or ports.

You may replace * with a specific IP address on the system. Such virtual hosts will only be used for HTTP requests received on connection to the specified IP address. However, it is additionally useful to use * on systems where the IP address is not predictable - for example if you have a dynamic IP address with your ISP, and you are using some variety of dynamic DNS solution. Since * matches any IP address, this configuration would work without changes whenever your IP address changes.

Related Topic