Search code examples
djangodnsapache2

Apache2 : Limit virtual host to one domain name


I want to do something but I can't find an answer (Maybe I badly searched). I don't know if it is possible so say me if it's not.

I would like to limit an Apache virtual host to one and only domain name : bde.yggdrasil.cafe. So that if the user try to access this website using 90.90.3.57 or another domain name it is listed as not existing website. Here is my extra/bde.conf which is included in httpd.conf, you'll understand the problem:

<VirtualHost *:80>
    ServerName      bde.yggdrasil.cafe
    ServerAdmin     my@email.fr
    DocumentRoot    /srv/http/bdeweb

    #Some django config
    #[...]

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =bde.yggdrasil.cafe
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Listen 443
<VirtualHost *:443>
    ServerName      bde.yggdrasil.cafe
    ServerAdmin     my@email.fr
    DocumentRoot    /srv/http/bdeweb

    #Some django config
    #[...]

    SSLEngine on
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/bde.yggdrasil.cafe/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/bde.yggdrasil.cafe/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

If I use this code and I try to connect to 90.90.3.57 or yggdrasil.cafe using http protocol my request is caught by Django which returns an error 400 (It is good but not what I want to get). if I connect to bde.yggdrasil.cafe using http it redirect me correctly to https.

DNS Redirection

I think it can be useful so I give you my DNS Redirections:
yggdrasil.cafe      --> 90.90.3.57
bde.yggdrasil.cafe  --> yggdrasil.cafe

Removing Django config

If I remove the Django configuration in my HTTP virtual host and I use the following file:
<VirtualHost *:80>
    ServerName      bde.yggdrasil.cafe

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =bde.yggdrasil.cafe
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Listen 443
<VirtualHost *:443>
    ServerName      bde.yggdrasil.cafe
    ServerAdmin     my@email.fr
    DocumentRoot    /srv/http/bdeweb

    #Some django config
    #[...]

    SSLEngine on
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/bde.yggdrasil.cafe/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/bde.yggdrasil.cafe/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

If I try to connect to my server using 90.90.3.57 or yggdrasil.cafe I fall on "Indexes of /" (Which is bad, really bad).

I would like Apache to ignore the connection if the user doesn't user the subdomain "bde.yggdrasil.cafe".

Why I want to do that ?

This domain is for all my stuff, including a future personal showcase website I would like to host on "yggdrasil.cafe" domain name (without subdomain). So the important is not the result but I would like to know if it is possible to add an other VirtualHost on "yggdrasil.cafe" domain name afterward without Django catching it.

Thanks for your time


Solution

  • Solution

    As said by Dusan Basic

    Of course, just add another VirtualHost with ServerName yggdrasil.cafe; for the beginning you can have simple RewriteRule ^ - [F]

    It fixed the problem :)