Search code examples
apacheportvirtualhostdomain-name

Apache Virtual Hosts on different domain names AND ports


I use a Ubuntu 16.04 server VM on which I have to configure the following domains:

  • maindomain.com:80
  • otherport.com:8080

Each domain pointing to the VM's IP, but to a different directory obviously.

I managed to get bind9 to make these domains point to the VM's IP when the VM is the DNS server, and I configured Apache to get the following results:

Good:

  • maindomain.com:80 returns maindomain's index
  • otherport.com:8080 returns otherport's index

Bad:

  • maindomain.com:8080 returns otherport's index
  • otherport.com:80 returns maindomain's index

If I put both on port 80, each is separated, but if I do different ports it seems that Apache just cares about the port.

How could I block the access to maindomain.com:8080 and otherport.com:80?

maindomain.com.conf file:

<VirtualHost maindomain.com:80>
    ServerAdmin webmaster@localhost
    ServerName maindomain.com
    ServerAlias www.maindomain.com maindomain.com
    DocumentRoot "/var/www/html/maindomain"
    <Directory /var/www/html/maindomain>
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

otherport.com.conf file:

<VirtualHost otherport.com:80>
    ServerAdmin webmaster@localhost
    ServerName otherport.com
    ServerAlias www.otherport.com otherport.com
    DocumentRoot "/var/www/html/otherport"
    <Directory /var/www/html/otherport>
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Solution

  • I managed to get it done, but I think it's more of a dirty hack than an actual solution. I made two more virtual hosts like so :

    maindomain.com.trap.conf

    <VirtualHost *:8080>
        ServerAdmin webmaster@localhost
        ServerName maindomain.com
        ServerAlias www.maindomain.com maindomain.com
        DocumentRoot "/var/www/html/maindomain"
        <Directory /var/www/html/maindomain>
            Require all denied
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    The other one having the names and port switched.
    By the way, I left <VirtualHost *:80> and <VirtualHost *:8080> in the first two .conf files I mentioned in my first post.