diff --git a/roles/nginx/templates/django_default_project.j2 b/roles/nginx/templates/django_default_project.j2 index 9d270338..a0666aae 100644 --- a/roles/nginx/templates/django_default_project.j2 +++ b/roles/nginx/templates/django_default_project.j2 @@ -10,6 +10,14 @@ server { listen 80; server_name {{ nginx_server_name }}; server_tokens off; + + # Terminate the request immediately if a request uses the IP address. + # This stops Invalid HTTP_HOST header exceptions being raised by Django. + + if ($host !~* ^({{ nginx_server_name }})$ ) { + return 444; + } + return 301 https://$server_name$request_uri; } @@ -31,6 +39,13 @@ server { ssl_dhparam /etc/ssl/certs/dhparams.pem; {% endif %} + # Terminate the request immediately if a request uses the IP address. + # This stops Invalid HTTP_HOST header exceptions being raised by Django. + + if ($host !~* ^({{ nginx_server_name }})$ ) { + return 444; + } + # Prevent MIME type sniffing for security add_header X-Content-Type-Options "nosniff"; @@ -95,7 +110,7 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; - proxy_set_header Host $http_host; + proxy_set_header Host $host; proxy_redirect off; # Try to serve static files from nginx, no point in making an