-1

I currently have a dedicated server that has two static IPs pointing to it, and two domain names pointing to their respective static IPs. Both of those domains have virtual hosts, and both of those domains resolve to the correct Apache directory. Let's call them domain1 (Associated to IP1 through DNS) and domain2(Associated to IP2 through DNS). The hostname is set to the fully qualified domain name (www.domain1.com)

The problem is, if I attempt to navigate through either IPs on a browser, it always refers me to domain1. I would like this to not be the case. I would like direct IPs to either resolve to an error, or to resolve to their specific domains. What am I doing wrong?

EDIT: As asked by a commenter, here are both of the virtual host files. 000-default.conf is empty.

domain1.com.conf, note that this one is a Symfony installation and runs without www:

<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias domain1.com

    DocumentRoot /var/www/html/domain1/web
    <Directory /var/www/html/domain1/web>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/html/domain1>
    #     Options FollowSymlinks
    # </Directory>


    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName domain1.com
    ServerAlias domain1.com

    SSLEngine on

    SSLCertificateFile "/etc/ssl/certs/domain1_com.pem"

    SSLCertificateKeyFile "/etc/ssl/private/domain1.key"

    SSLCACertificateFile "/etc/ssl/certs/domain1_cert.pem"

    DocumentRoot /var/www/html/domain1/web
    <Directory /var/www/html/domain1/web>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/html/domain1>
    #     Options FollowSymlinks
    # </Directory>


    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

www.domain2.conf, this one doesn't have a ssl certificate yet, so I only put in the virtual host for port 80;

<VirtualHost *:80>
    ServerName domain2.com
    ServerAlias www.domain2.com

    DocumentRoot /var/www/html/domain2
    <Directory /var/www/html/domain2>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/html/domain2>
    #     Options FollowSymlinks
    # </Directory>


    ErrorLog /var/log/apache2/domain2.log
    CustomLog /var/log/apache2/domain2.log combined
</VirtualHost>
Sefam
  • 111
  • 3
  • 8
  • add the apache config for both vhosts to your question. – Gerald Schneider Jun 20 '17 at 13:56
  • You mean the virtual host files I assume? – Sefam Jun 20 '17 at 13:57
  • Yes, I suspect your virtual hosts are listening on all IP addresses, so when you connect via IP you are just given the first virtual host (as it can't match based on ServerName in that case) – USD Matt Jun 20 '17 at 14:01
  • I suppose that must be the case. I assume I would need to put in something in 000-default. Is this correct? But if so, what ServerName do I give it? – Sefam Jun 20 '17 at 14:04

1 Answers1

2
ServerName domain1.com
ServerAlias domain1.com

Alias is only needed to add additional hostnames. You do not need to add an alias for something that is already the ServerName.

Regarding the main problem, you have two virtual hosts listening on any IP address. When a request comes in, Apache will look at the virtual hosts listening on that address (which in your case is both, regardless of the IP address you type in), and will try to match the servername. If it can't, it will just return the first virtual host.

What you want is something like the following -

<VirtualHost ip.address.one:80>
    ... Website One ...
</VirtualHost>

<VirtualHost ip.address.two:80>
    ... Website Two ...
</VirtualHost>

Same for the SSL (but with port 443 obviously)

That way, when a request comes in by IP address, it will only match the correct virtual host.

USD Matt
  • 5,321
  • 14
  • 23