Two static IPs resolving to the same Apache directory

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>
-1
задан 20 June 2017 в 17:02
1 ответ
ServerName domain1.com
ServerAlias domain1.com

Псевдоним нужен только для добавления дополнительных имен хостов. Вам не нужно добавлять псевдоним для того, что уже является ServerName.

Что касается основной проблемы, у вас есть два виртуальных хоста, которые прослушивают любой IP-адрес. Когда поступает запрос, Apache будет смотреть на виртуальные хосты, прослушивающие этот адрес (который в вашем случае является обоими, независимо от введенного вами IP-адреса), и попытается сопоставить имя сервера. Если не может, он просто вернет первый виртуальный хост.

Вам нужно что-то вроде следующего -

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

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

То же самое для SSL (но, очевидно, с портом 443)

Таким образом, когда запрос входит по IP-адресу, он будет соответствовать только правильному виртуальному хосту.

2
ответ дан 5 December 2019 в 19:22

Теги

Похожие вопросы