DisallowedHost at / Invalid HTTP_HOST header: 'www.example.com'. You may need to add 'www.example.com' to ALLOWED_HOSTS

孤街浪徒 提交于 2019-12-25 18:22:26

问题


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;
  [...]
}


来源:https://stackoverflow.com/questions/52229388/disallowedhost-at-invalid-http-host-header-www-example-com-you-may-need-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!