Skip to content

Commit c0d9373

Browse files
authored
Merge pull request #178 from StuartMacKay/invalid-host-header
Stop invalid host header errors being raised by Django
2 parents debc7cf + c36e9d4 commit c0d9373

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

roles/nginx/templates/django_default_project.j2

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ server {
1010
listen 80;
1111
server_name {{ nginx_server_name }};
1212
server_tokens off;
13+
14+
# Terminate the request immediately if a request uses the IP address.
15+
# This stops Invalid HTTP_HOST header exceptions being raised by Django.
16+
17+
if ($host !~* ^({{ nginx_server_name }})$ ) {
18+
return 444;
19+
}
20+
1321
return 301 https://$server_name$request_uri;
1422
}
1523

@@ -31,6 +39,13 @@ server {
3139
ssl_dhparam /etc/ssl/certs/dhparams.pem;
3240
{% endif %}
3341

42+
# Terminate the request immediately if a request uses the IP address.
43+
# This stops Invalid HTTP_HOST header exceptions being raised by Django.
44+
45+
if ($host !~* ^({{ nginx_server_name }})$ ) {
46+
return 444;
47+
}
48+
3449
# Prevent MIME type sniffing for security
3550
add_header X-Content-Type-Options "nosniff";
3651

@@ -95,7 +110,7 @@ server {
95110

96111
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
97112
proxy_set_header X-Forwarded-Proto https;
98-
proxy_set_header Host $http_host;
113+
proxy_set_header Host $host;
99114
proxy_redirect off;
100115

101116
# Try to serve static files from nginx, no point in making an

0 commit comments

Comments
 (0)