VirtualHost fails with multiple interfaces

apache-2.4centos7

I have instance with multiple interfaces and I am trying to configure Apache with IP-based virtual hosting to serve multiple websites, but for some reason it just returns the main document root.

I followed this tutorial to get multiple interfaces working.

What is wrong: The website should show /var/www/mydomain/index.html, but instead it shows /var/www/html/index.html.

What I have tried: Using *:80 in VirtualHost, which works but the goal is to have multiple VirtualHosts each from different IP/interface. Removing port from VirtualHost: Doesn't do anything. Changing Listen 80 to Listen 92.168.1.231:80, which fails with Cannot assign requested address: AH00072: make_sock: could not bind to address 92.168.1.231:80. This is worrying and I haven't found any explanation what is happening. There is nothing else listening the port and if I just listen all 80 ports it works fine.

Info on my config:

/sbin/ifconfig

eth0: flags=4163 mtu 1500
inet 192.168.1.231 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::f816:3eff:fee7:b528 prefixlen 64 scopeid 0x20
ether fa:16:3e:e7:b5:28 txqueuelen 1000 (Ethernet)
RX packets 8812 bytes 6501811 (6.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3884 bytes 331594 (323.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth1: flags=4163 mtu 1500
inet 192.168.1.232 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::f816:3eff:fea1:e658 prefixlen 64 scopeid 0x20
ether fa:16:3e:a1:e6:58 txqueuelen 1000 (Ethernet)
RX packets 10623 bytes 788238 (769.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5290 bytes 1647702 (1.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth2: flags=4163 mtu 1500
inet 192.168.1.233 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::f816:3eff:fead:8432 prefixlen 64 scopeid 0x20
ether fa:16:3e:ad:84:32 txqueuelen 1000 (Ethernet)
RX packets 6520 bytes 396490 (387.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 51 bytes 4973 (4.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth3: flags=4163 mtu 1500
inet 192.168.1.234 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::f816:3eff:fe4a:8dc7 prefixlen 64 scopeid 0x20
ether fa:16:3e:4a:8d:c7 txqueuelen 1000 (Ethernet)
RX packets 33438 bytes 3106902 (2.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26161 bytes 16106821 (15.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 1992 bytes 199852 (195.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1992 bytes 199852 (195.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

sudo httpd -S

VirtualHost configuration:
92.168.1.231:80 www.mydomain.com (/etc/httpd/conf.d/sites.conf:1)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48

cat /etc/httpd/conf.d/sites.conf

<VirtualHost 92.168.1.231:80>  
    ServerName www.mydomain.com  
    ServerAlias mydomain.com  
    DocumentRoot "/var/www/mydomain/"  
    ErrorLog "/var/log/httpd/mydomain/error_log"  
    TransferLog "/var/log/httpd/mydomain/access_log"  
</VirtualHost>  

sudo netstat -pntl (when having Listen 80 in httpd.conf and Apache running)

Active Internet connections (only servers)  
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      1191/memcached  

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd  

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1221/sshd  

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2090/master  

tcp6       0      0 :::3306                 :::*                    LISTEN      1386/mysqld  

tcp6       0      0 ::1:11211               :::*                    LISTEN      1191/memcached  

tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd  

tcp6       0      0 :::80                   :::*                    LISTEN      8094/httpd  

tcp6       0      0 :::22                   :::*                    LISTEN      1221/sshd  

tcp6       0      0 ::1:25                  :::*                    LISTEN      2090/master

httpd.conf is pretty much vanilla except changing admin email, removing auto-indices from main document root and setting server name.

Best Answer

In the main server config you should have:

Listen 80

This instructs apache to listen on all interfaces (and thus on all IPs). Only change it, if you do not want apache to listen on all interfaces/IPs.

Your VHosts should be configured as follows:

<VirtualHost 192.168.1.231:80>
  ServerName mydomain1.com
  DocumentRoot /var/www/mydomain1
  ...
</VirtualHost>
<VirtualHost 192.168.1.232:80>
  ServerName mydomain2.com
  DocumentRoot /var/www/mydomain2
  ...
</VirtualHost>

Also check that your local dns server or /etc/hosts file is pointing to the correct IP address.

Related Topic