I have moved a server configuration to a new server and at the same time configured it with apache mod_proxy to access a gunicorn/django webserver (inside a docker container on port 8000). Previous server was using apache mod_wsgi to access django directly.
So I changed some virtualhosts configuration but I do not understand why now some requests are going through apache to django, when the request servername is not one of my DNS and virtualhosts configurations...
For now, django stops them with Invalid HTTP_HOST header
errors, but I would like to filter them in apache configuration directly, and to understand how such scanning requests might pass DNS and apache configurations.
Here is what looks like a typical django error I received:
Report at /
Invalid HTTP_HOST header: 'sprucetips.cyou'. You may need to add 'sprucetips.cyou' to ALLOWED_HOSTS.
Request Method: GET
Request URL: https://sprucetips.cyou/
Django Version: 2.2.28
Python Executable: /usr/local/bin/python
Python Version: 3.5.10
...
Request information:
USER: [unable to retrieve the current user]
GET: No GET data
POST: No POST data
FILES: No FILES data
COOKIES: No cookie data
META:
HTTP_ACCEPT_ENCODING = 'gzip'
HTTP_CONNECTION = 'Keep-Alive'
HTTP_HOST = 'sprucetips.cyou'
HTTP_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
HTTP_X_FORWARDED_FOR = '34.220.181.252'
HTTP_X_FORWARDED_HOST = 'sprucetips.cyou'
HTTP_X_FORWARDED_PROTO = 'https'
HTTP_X_FORWARDED_SERVER = 'mydomain.ca'
PATH_INFO = '/'
QUERY_STRING = ''
RAW_URI = '/'
REMOTE_ADDR = '172.23.0.1'
REMOTE_PORT = '47332'
REQUEST_METHOD = 'GET'
SCRIPT_NAME = ''
SERVER_NAME = '0.0.0.0'
SERVER_PORT = '8000'
SERVER_PROTOCOL = 'HTTP/1.1'
SERVER_SOFTWARE = 'gunicorn/20.1.0'
...
My DNS is configured on two domain names, mydomain.ca and mydomain.com. I previously set a DNS config for each subdomain but now I only set mydomain.ca
*.mydomain.ca
mydomain.com
*.mydomain.com
all to the same IP. Can this be a problem?
And there are my virtualhosts:
First 001-mydomain.ca.conf
to redirect any 80 port request to 443 port
<VirtualHost *:80>
ServerName mydomain.ca
ServerAlias www.mydomain.com www.mydomain.ca mydomain.com sub1.mydomain.ca sub2.mydomain.ca sub3.mydomain.ca
ErrorLog ${APACHE_LOG_DIR}/mydomain.ca.error.log
LogLevel info
CustomLog ${APACHE_LOG_DIR}/mydomain.ca.access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.ca [OR]
RewriteCond %{SERVER_NAME} =mydomain.com [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond %{SERVER_NAME} =mydomain.ca
RewriteRule ^ https://mydomain.ca%{REQUEST_URI} [END,QSA,R=permanent]
RewriteCond %{SERVER_NAME} =sub1.mydomain.ca
RewriteRule ^ https://sub1.mydomain.ca%{REQUEST_URI} [END,QSA,R=permanent]
RewriteCond %{SERVER_NAME} =sub2.mydomain.ca
RewriteRule ^ https://sub2.mydomain.ca%{REQUEST_URI} [END,QSA,R=permanent]
RewriteCond %{SERVER_NAME} =sub3.mydomain.ca
RewriteRule ^ https://sub3.mydomain.ca%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
010-mydomain.ca-ssl.conf
<VirtualHost *:443>
ServerName mydomain.ca
ServerAlias www.mydomain.ca sub1.mydomain.ca sub2.mydomain.ca
DocumentRoot /var/www/mydomain.ca
ErrorLog ${APACHE_LOG_DIR}/mydomain.ca.error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/mydomain.ca.access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.ca/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.ca/privkey.pem
RewriteEngine On
RewriteMap exceptions /var/www/mydomain.ca/maintenance_exceptions.map
RewriteCond /var/www/mydomain.ca/maintenance.html -f
RewriteCond /var/www/mydomain.ca/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteCond ${exceptions:%{REMOTE_ADDR}} !OK
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
ProxyRequests Off
ProxyPreserveHost On
RequestHeader set X_FORWARDED_PROTO 'https' env=HTTPS
ProxyPass /static/ !
ProxyPass /media/ !
ProxyPass /favicon.ico !
ProxyPass /maintenance.html !
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
Alias /favicon.ico /var/www/mydomain.ca/favicon.ico
Alias /media/ /var/www/mydomain.ca/media/
Alias /static/ /var/www/mydomain.ca/static/
<Directory /var/www/mydomain.ca>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/mydomain.ca/static>
Header set Cache-Control "max-age=86400, must-revalidate"
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName mydomain.com
ServerAlias www.mydomain.com
RewriteEngine On
RewriteRule ^ https://mydomain.ca%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
One of the subdomains is redirected to another webserver with 011-sub3.mydomain.ca-ssl.conf
<VirtualHost *:443>
ServerName sub3.mydomain.ca
ErrorLog ${APACHE_LOG_DIR}/sub3.mydomain.ca.error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/sub3.mydomain.ca.access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.ca/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.ca/privkey.pem
ProxyPreserveHost On
ProxyRequests off
AllowEncodedSlashes NoDecode
ProxyPass /.well-know !
ProxyPass /robots.txt !
ProxyPass / http://localhost:3000/ nocanon
</VirtualHost>
And I finally manage others subdomains and direct IPrequests in 020-default.mydomain.ca.conf
<VirtualHost *:80 *:443>
ServerName default.mydomain.ca
ServerAlias *.mydomain.ca *.mydomain.com
Redirect 404 /
CustomLog ${APACHE_LOG_DIR}/mydomain.ca.other.log combined
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName <IP>
Redirect 403 /
ErrorDocument 403 "Direct IP access not allowed"
UseCanonicalName Off
CustomLog ${APACHE_LOG_DIR}/mydomain.ca.other.log combined
</VirtualHost>
Any tip, advise or magic answer would be so appreciated. Thanks.