DisallowedHost at / Invalid HTTP_HOST header: '

2019-09-20 18:07发布

问题:

I have deployed a Django project in digitalocean ubuntu 16.04.My domain name is like example.com

Because I want to add www. in front of my domain name so that if input www.example.com it can also direct to my website.

I bought my domain name from hostgator.

I first added www.example.com in my digitalocean DNS records to let www.example.com direct to my ip address.

Then I added www.example.com to myproject.setting in django to

 ALLOWED_HOSTS = ['.example.co','www.example.com']

Third I went to

sudo nano /etc/nginx/sites-available/myproject

to add www.example.com in nginx

server {
    listen 80;
    server_name example.com www.example.com;
}

Then I test and restart nginx and gunicorn without any issue.

But I still received the issue DisallowedHost at / Invalid HTTP_HOST header: 'www.example.com'. You may need to add 'www.example.com' to ALLOWED_HOSTS when use www.example.com

Only example.com works.

Any friend can help?

Thank you so much!

回答1:

I just solved it by following this tutorial https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04#configure-nginx-redirect

One thing need to be noticed is the form:

You should use:

server {
  server_name www.domain.com;
  return 301 $scheme://domain.com$request_uri;
}
server {
  server_name domain.com;
  [...]
}