Rom1
Rom1

Reputation: 386

Apache conf VirtualHost ignored

I'm running apache on a CentOS 7. I have 2 config files in /etc/httpd/conf.d :

As they are similar, I show you only one of the files :

<VirtualHost *:443>
    ErrorLog "logs/my_site_test.log"
    SSLCertificateFile /etc/ssl/host/host.crt
    SSLCertificateKeyFile /etc/ssl/host/host.key

    Alias /my-site/test/static /var/www/my_site_test/static/
    <Directory /var/www/my_site_test/static>
        Require all granted
    </Directory>

    WSGIScriptAlias /my-site/test /var/www/my_site_test/app/wsgi.py
    <Directory /var/www/my_site_test>
        Require all granted
    </Directory>
</VirtualHost>

Currently, VirtualHost in A10-my_site_test.conf is ignored. If I go to https://my-server.com/my-site/test, I've got a 404, but https://my-server.com/my-site/dev is working fine. If I remove A10-my_site_dev.conf and restart apache, https://my-server.com/my-site/test works. I understood that it takes only the first VirtualHost apache finds.

I have the feeling that VirtualHost in my ssl.conf is ignored too cause, if I move SSLCertificateFile and SSLCertificateKeyFile to ssl.conf, I have the following error :

enter image description here

I tried not to use VirtualHost, and everything works fine in that case. I think all VirtualHost are ignored except the first.

I've got the same problem with other ports (like 80).

Another point : I read that I should have a ServerName. But I have the same ServerName for all my environments. I tried to put something random such as "my_site_test" as ServerName, but I've got a 404.

How could I configure apache to make my 2 files work ?

Upvotes: 0

Views: 443

Answers (1)

Nic3500
Nic3500

Reputation: 8591

This is not how VirtualHosts work. Especially with SSL.

So you have:

<VirtualHost *:443>
    CONFIGURATION FOR DEV
</VirtualHost>

<VirtualHost *:443>
    CONFIGURATION FOR TEST
</VirtualHost>

The only difference between both VirtualHosts is the logs and directory? That will not work.

Apache can distinguish between VirtualHost sections based on:

  • IP associated to the domain
  • Port
  • ServerName (for non-SSL configurations)

The way you configured it right now, Apache cannot distinguish between both VirtualHosts. So it takes the first one it finds. That explains the mix up with certificates.

Ex of a working configuration

# www.example1.com == 1.1.1.1
# www.example2.com == 2.2.2.2

<VirtualHost 1.1.1.1:443>
    ServerName www.example1.com

    # SSL CONFIG
    # LOGS CONFIG
    # OTHER CONFIGURATION

</VirtualHost>

<VirtualHost 2.2.2.2:443>
    ServerName www.example2.com

    # SSL CONFIG
    # LOGS CONFIG
    # OTHER CONFIGURATION

</VirtualHost>

Read the documentation (again?), it will do you good: https://httpd.apache.org/docs/2.4/vhosts/examples.html

There are posts on this site explaining the finer points of SSL VirtualHosts configuration (I even wrote some myself).

Upvotes: 1

Related Questions