【发布时间】:2019-11-22 21:41:25
【问题描述】:
我有一个使用 NGINX 和 gunicorn 在 ubuntu 服务器上运行的小型 Django 项目。
当用户访问 www.example.com 时一切正常,但是,当用户访问 example.com 而不是 www.example.com 时,它就崩溃了。我在 fetch 命令中使用了一些 javascript,从我的 API (Django REST) 获取数据并返回:
Access to fetch at 'https://www.example.com/builds/apidata/4'
from origin 'https://example.com' has been blocked by CORS
policy: No 'Access-Control-Allow-Origin' header is present on the requested
resource. If an opaque response serves your needs, set the request's mode to
'no-cors' to fetch the resource with CORS disabled.
我找到的最简单的解决方案是从我的ALLOWED_HOSTS 文件中的ALLOWED_HOSTS 中删除example.com,但是当用户忘记www. 时,他们将收到404 错误。
CORS 错误消息还指出我可以更改请求模式,但我不知道这样做的安全隐患..
我还尝试在我的域设置上使用从 example.com 到 www.example.com 的重定向,但这似乎也不起作用。
任何建议将不胜感激!
【问题讨论】:
标签: javascript django django-rest-framework cors