Monday, July 2, 2018

Re: Invalid HTTP_HOST header when website being accessed by public IP

On maandag 2 juli 2018 17:25:20 CEST Kasper Laudrup wrote:

> Instead I added the following to my HTTPS server section:
>
> if ($host != my-website.org {
> return 404;
> }
>
> Seems to solve my problem just fine. Letsencrypts certbot had already
> done something similar for the HTTP section redirect.

The only reason to set it up like that for HTTPS is that it's possible the SNI
name differs from the HTTP Host header. For HTTP redirects it makes no sense:
the HTTP header is in plain text and is used to determine the server block to
pick. So putting an if statement there, is just doing it again, on every
request, because electrons are cheap. Save the electrons!

Anyhow - instead of return 404, I would do:

return 301 https://$server_name$request_uri

How I normally set things up:

server {
listen 443 default_server ssl http2;
server_name localhost;

return 301 https://djangoserver.example.com$request_uri
}

server {
listen 443;
server_name djangoserver.example.com;

# ... django setup
}
--
Melvyn Sopacua

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1733971.5nBxyqRtuB%40fritzbook.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment