2

I am not much of an admin (nor of an native english speaker, for that matter) and I am struggling with a simple task: add a subdomain on Apache 2.4. Here are my conf files:

<VirtualHost *:80>
    ServerName domain.io
    ServerAlias domain.io
    ServerAlias *.domain.io
    DocumentRoot /var/www/html/david
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

And the second:

<VirtualHost *:80>
    ServerName sub.domain.io
    ServerAlias sub.domain.io
    ServerAlias *.sub.domain.io
    DocumentRoot /var/www/html/ness_pro
    <Directory /var/www/html/ness_pro>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

[the problem] Every time I try to access sub.domain.io with a browser, I end up on the page related to domain.io. I followed the guidelines of apache 2.4 (i.e. edit in sites-available -> a2ensite -> systemctl restart).

[about my DNS conf] I redirected sub.domain.io to domain.io on my DNS provider (Gandi). It points to the right url. I naively thought that apache would analyze the request url to dispatch between the different VHosts but it seems that I've been naive. Is there some easy way to fix this (I always found apache url rewriting pretty obscure)?

[env] Debian 9 (raspbian) // Apache 2.4

[edit] Since everyone is asking. Here is my DNS record

@ 10800 IN SOA ns1.gandi.net. hostmaster.gandi.net. 1542647212 10800 3600 604800 10800
@ 10800 IN A 77.193.111.117
@ 10800 IN MX 10 spool.mail.gandi.net.
@ 10800 IN MX 50 fb.mail.gandi.net.
@ 10800 IN TXT "v=spf1 include:_mailcust.gandi.net ?all"
blog 10800 IN CNAME blogs.vip.gandi.net.
sub 10800 IN CNAME webredir.vip.gandi.net.
webmail 10800 IN CNAME webmail.gandi.net.
www 10800 IN CNAME webredir.vip.gandi.net.
11
  • Hi David, I am not exactly an Apache guru, but I think the order matters here, and I think your *.domain.io is matching before the sub.domain.io. Why don't you try to set both virtual hosts in the same file, but put the sub.domain.io above the *.domain.io Nov 19, 2018 at 20:16
  • On Debian I am almost sure you can control this order with the filename also, the default is usually 000-default, you can use 000 for sub.domain.io and 001 for domain.io, check it out Nov 19, 2018 at 20:18
  • I tried to invert the order while putting both on the same conf file but it did not change a thing
    – zar3bski
    Nov 19, 2018 at 20:20
  • The redirection you are performing, is some Gandi URL redirect or is it a CNAME DNS record? Nov 19, 2018 at 20:25
  • not sure what a "CNAME DNS record" is. I would say a Gandi URL redirect: everytime someone is looking for sub.domain.io, he is redirected to the same public ip of domain.io.
    – zar3bski
    Nov 19, 2018 at 20:28

2 Answers 2

4

This may not answer your question, but, I prepared your same scenario, used your same files changing only the DocumentRoot and the order is reversed as I told you in the comments:

# /etc/apache2/sites-enabled/000-domain.io.conf
<VirtualHost *:80>
    ServerName sub.domain.io
    ServerAlias sub.domain.io
    ServerAlias *.sub.domain.io
    DocumentRoot /var/www/html/serverfault/sub
    <Directory /var/www/html/serverfault/sub>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName domain.io
    ServerAlias domain.io
    ServerAlias *.domain.io
    DocumentRoot /var/www/html/serverfault/parent
    ErrorLog /var/log/apache2/error.example.com.log
    CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

Created a test pages in each DocumentRoot:

echo "parent" > /var/www/html/serverfault/parent/index.html echo "sub" > /var/www/html/serverfault/sub/index.html chown www-data:www-data /var/www/html/serverfault/ -R

To simulate the DNS I edited the /etc/hosts file including:

# ...
127.0.0.1   domain.io
127.0.0.1   sub.domain.io
127.0.0.1   www.domain.io
127.0.0.1   www.sub.domain.io
# ...

Restarted Apache: systemctl restart apache2

And it worked as expected: Querying the virtualhosts

Moreover, using the "Tamper Data" plugin for firefox I started manipulating the Host header on each request and the response changes in relation to it as expected.

I don't see why yours would not be working. To see if you have any outstanding alerts in the apache service you could use journalctl -u apache2

DNS

I used the DNS from my domain to be more realistic to your scenario and it worked the same way as using the hosts file, using CNAME or using A records, both of them worked correctly.

As a personal preference, I would say that the A record should be a name for the server, and then CNAMEs for the different host headers, something like (your server for me is 1.1.1.1):

webserver01 10800 IN A 1.1.1.1
www 10800 IN CNAME webserver01.domain.io.
sub 10800 IN CNAME webserver01.domain.io.

Or in your case you may point the CNAMES to @, where you set the IP:

@ 10800 IN SOA ns1.gandi.net. hostmaster.gandi.net. 1542647212 10800 3600 604800 10800
@ 10800 IN A 77.193.111.117

sub 10800 IN CNAME @
www 10800 IN CNAME @

I don't know if that syntax is valid, maybe:

sub 10800 IN CNAME domain.io.
www 10800 IN CNAME domain.io.

(mind the trailing .)

A third option would be generating all A records, pointing to the direct IP, maybe just harder to maintain in a bigger environment but equally effective:

sub 10800 IN A 1.1.1.1
www 10800 IN A 1.1.1.1

One thing for you to consider is that sub is not a subdomain of domain.io as you exposed it, it is only a host for domain.io. You should create a zone to make it a subdomain.

Gandi

I am pretty sure that webredir.vip.gandi.net. is the responsible for you not getting the expected behavior, if you see the documentation, the redirection type that you are using is the one that says:

When to use? For traditional web forwarding (ex. to a preexisting website, hosted on another page. This is commonly used to forward a domain to a blog or other free personal page that is already hosted at a given address.)

It looks like a basic redirection for the simplest hosting they offer, if you go to the item 3 in the same page, it says:

When to use? If your host gives you an IP address for your HTTP server (or if you host it yourself using Gandi's hosting), or tells you to make a particular “A”, “MX”, or “CNAME” (etc.) record in your zone file.

Which, from the information we have, I think is your case, isn't it?

I hope this information and testing helps

6
  • Yeah, it does help. by editing my host, it works -> DNS related, then. I used the "hidden" redirection so I'll switch to the "permanent" and see what's what. I do not dare to edit the DNS record directly, for i) I'm not familiar with the inherent concepts yet ii) and I do not have a static ip (uploading it to gandi's API with a CRON python script every 15 minutes) (the server is, basically, a raspberry pi at my place). I don't wan't to make a mess on my existing config so I'll read first. Many Thanks @Jorge
    – zar3bski
    Nov 21, 2018 at 21:46
  • Anytime David, let me know if you need further help Nov 21, 2018 at 21:53
  • thanks. I'll try your solutions one by one. Every change I make need to to propagate, right?
    – zar3bski
    Nov 21, 2018 at 22:00
  • In DNS yes, but is usually fast, you can use dig or nslookup to the 8.8.8.8 to verify propagation Nov 21, 2018 at 23:06
  • Thanks Jorge, it solved my problem (and I know how to edit DNS records now :-) )
    – zar3bski
    Nov 22, 2018 at 16:44
0

The first entry contains ServerAlias *.domain.io. The address sub.domain.io matches *.domain.io.

If you want to to use the name sub.domain.io, that entry needs to be first, or at least before *.domain.io.

7
  • just tried to change the order: it did not change a thing :/
    – zar3bski
    Nov 19, 2018 at 20:20
  • You mention somthing about "redirecting sub.domain.io to domain.io". What exactly does that mean?
    – RalfFriedl
    Nov 19, 2018 at 20:24
  • that means that everytime someone is looking for sub.domain.io, he is redirected to the same public ip of domain.io. But the urls requests in the http headers are different (I checked)
    – zar3bski
    Nov 19, 2018 at 20:26
  • You can add \"%{Host}i\" to your Logformat to log the host the client sends. You may also try to remove the ServerAlias that is the same as the ServerName. I don't think it's a problem, but it's at least redundant.
    – RalfFriedl
    Nov 19, 2018 at 20:29
  • What exact DNS configuration you used? A record or CNAME record? If you added A record only, then it's fine. Question: Does the system behaving the same way if you will remove the second part about sub.domain.io completely? I am asking because it looks like *.domain.io rule is hitting all the time. Nov 19, 2018 at 20:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .