Invalid HTTP_HOST header: '.your-domain.com'. The domain name provided is not valid according to RFC 1034/1035 (You may need to add u'domain.com' to ALLOWED_HOSTS.).



I have several Django projects published and my mailbox and log files are constantly inundated with spider errors and hacking attempts to connect to my applications. These error messages have an email subject: "[Django] ERROR (EXTERNAL IP): Invalid HTTP_HOST ...". 

So after spending a long time with this, I solved this problem with Apache (Require).

The correct format for "Require" and "SetEnvIfExpr" is:

^[^_]+ would match a string of 1 or more character containing any character except underscore.

<VirtualHost xxx.xxx.xxx.xxx:80>

    ...


    SetEnvIfNoCase Host "^[^_]+\.my-domain\.com" VALID_HOST

    <Location />

        <RequireAll>

            Require all granted

            Require env VALID_HOST

        </RequireAll>

    </Location>


    ...

</VirtualHost>

Or to be more safe we can apply it to wsgi.py file:

<VirtualHost xxx.xxx.xxx.xxx:80>

    ...


    SetEnvIfNoCase Host "^[^_]+\.my-domain\.com" VALID_HOST

    <Files wsgi.py>

        <RequireAll>

            Require all granted

            Require env VALID_HOST

        </RequireAll>

    </Files>


    ...

</VirtualHost>

OR with Require expr

<VirtualHost xxx.xxx.xxx.xxx:80>

    ...

    <Files wsgi.py>

        Require expr %{HTTP_HOST} =~ m#^[^_]+\.my-domain\.com#

    </Files>


    ...

</VirtualHost>

Based on:

Blocking of Robots

https://httpd.apache.org/docs/2.4/rewrite/access.html#blocking-of-robots

And

Environment Variables in Apache

https://httpd.apache.org/docs/2.4/env.html

Links to resources:






Comments

Popular posts from this blog

Installing the Certbot Let’s Encrypt Client for NGINX on Amazon Linux 2

psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: ERROR: failed to authenticate with backend using SCRAM DETAIL: valid password not found

Deploy Nuxt.js app using Apache 2