2

I have two sites on a server: dev.somedomain.com and somedomain.com - the latter has a ServerAlias of *.somedomain.com. All domains are SSL-enabled, and all requests to port 80 is redirected to https. The somedomain.com site is a proxy of an old site, on a different server, that is about to be decommisioned.

dev.somedomain.com works like a charm

http://somedomain.com is for some reason "caught" by the 000-default.conf configuration (https is not), but http://www.somedomain.com (the serveralias *.somedomain.com) is NOT "caught" by 000-default.conf, and is working as expected.

If I disable the 000-default.conf, everything seems to work, except for https://dev.somedomain.com/phpmyadmin (which DOES work with 000-default enabled - I think I understand why)

I've searched high and low, but been unable to figure this out.

somedomain.conf:

<VirtualHost *:80>
    ServerName dev.somedomain.com

    Redirect 301 / https://dev.somedomain.com
</VirtualHost>

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

    Redirect 302 / https://somedomain.com/
</VirtualHost>

And the 000-default.conf, which seems to break somedomain.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

somedomain-ssl.conf: (this one does not seem to have any problems, but included here to be sure)

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName dev.somedomain.com

        DocumentRoot "/var/www/somedomain/wwwroot"
        Alias /phpmyadmin /usr/share/phpmyadmin
        Alias /index.php /var/www/somedomain/index.php
        Alias /import-data.php /var/www/somedomain/import.php
        <Directory /var/www/somedomain/wwwroot>
            Options FollowSymLinks
            AllowOverride None

            Require all granted

            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ index.php?url=$1 [QSA]
        </Directory>

        ErrorLog /var/www/somedomain/logs/error.log
        CustomLog /var/www/somedomain/logs/access.log combined

        ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php7.0-fpm.sock|fcgi://localhost/var/www/somedomain/

        SSLCertificateFile /etc/letsencrypt/live/dev.somedomain.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/dev.somedomain.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>

    <VirtualHost *:443>
        ServerName somedomain.com

        <Location "/">
        ProxyPreserveHost On

        #anonymised IP to protect the guilty:
        ProxyPass http://172.0.0.10/
        ProxyPassReverse http://somedomain.com/

        AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html
        Substitute "s|http://somedomain.com/|/|iq"
        SetOutputFilter DEFLATE
        </Location>

        SSLCertificateFile /etc/letsencrypt/live/dev.somedomain.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/dev.somedomain.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
</IfModule>

Long question short: why does http://somedomain.com show apaches default page, while http://www.somedomain.com is - as expected - redirected to https://somedomain.com?

I assume the answer is obvious, and I'm just blind, but I really have no idea what's going on.

Thank you

EDIT

VirtualHost configuration:
*:443                  is a NameVirtualHost
         default server dev.somedomain.com (/etc/apache2/sites-enabled/somedomain-ssl.conf:2)
         port 443 namevhost dev.somedomain.com (/etc/apache2/sites-enabled/somedomain-ssl.conf:2)
         port 443 namevhost somedomain.com (/etc/apache2/sites-enabled/somedomain-ssl.conf:35)
*:80                   is a NameVirtualHost
         default server somedomain.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost somedomain.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost dev.somedomain.com (/etc/apache2/sites-enabled/somedomain.conf:1)
         port 80 namevhost somedomain.com (/etc/apache2/sites-enabled/somedomain.conf:7)
                 wild alias *.somedomain.com
2

First start with the output of the command apachectl -S. It shows you all your virtual hosts and the files they come from.

The first listed virtual host in the output is the 'default virtual host'. I am guessing this is the one in 000-default.conf. If so, then that virtual host has no ServerName and so will take the one from the global (server) context, or the hostname of the machine. If this is 'somedomain.com' then it means requests for this domain will land on this virtual host. If is is not the show us the output of apachectl -S to help troubleshoot.

Explanation: This is just the way virtual hosts work. Apachae tries to match the hostname used (actually the contents of the 'Host' HTTP header) against any ServerName or ServerAlias and once it finds a match that virtual host receives the request. If no match is found the first virtual host listed is used. So if you have two virtual host with the same ServerName only the first will can be accessed using that name.

Best practise: Give every virtual host a ServerName to make sure you explicitly know which requests will land on each one, and every time you make a configuration change run apachectl -S

  • apachectl -S added to question. I'm not sure I follow - the relevant virtualhosts all have servername? What I don't understand is why the fallback 000-default.conf is triggered by a request that has a matching vhost? – Vonsild Sep 9 '16 at 12:38
  • 1
    If you look at the directives between the <VirtualHost> tags in 000-default.conf you'll see theres is no explicit ServerName, so it is taking a default name from the global context. Look at the output, you have two virtual hosts with the same ServerName. – Unbeliever Sep 9 '16 at 13:09
  • Okay, yes, I see that. But it's the fallback that's only supposed to trigger, when there is no other match? And it's supposed to be the catch-all, and many places I read "do not mess with 000-default.conf" - adding an explicit ServerName to 000-default.conf would cause it to not work as intended? – Vonsild Sep 9 '16 at 13:15
  • No its not a fall back, The first ServerName or ServerAlias that matches is used, hence why you lost somedomain.com. Adding an Explicit ServerName to 000-default.com will indeed fix things, as long as its not the same as any of your others :) Using the IP of the server is a good choice normally. – Unbeliever Sep 9 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.