django www 与非 www 中间件身份验证问题
我的 Django 应用程序的行为一直不一致。
如果我在没有 www 的情况下登录,然后在前面加上 www,则它未经过身份验证,以及它们的所有组合。 (www.mydomain.com 和 mydomain.com 在身份验证方面就像不同的网站)
如果身份验证代码很重要,我根据这里的教程编写了一个中间件: http://onecreativeblog.com/post/59051248/django-login-required-middleware
到目前为止,我已经解决了强制附加 www 的问题,使用 PREPEND_WWW = True< /code>,但我仍然想了解这个问题;)
有人知道可能发生了什么吗? 提前致谢!
I have been having inconsistent behavior with my Django app.
If I login with no www, and then prepend www, it's not authenticated, and all the combinations thereof. (www.mydomain.com and mydomain.com like different sites in terms of auth)
If the authentication code is important, I wrote a middleware based on the tutorial here: http://onecreativeblog.com/post/59051248/django-login-required-middleware
So far I have fixed the issue forcing the appending of www, using PREPEND_WWW = True
, but I would still like to understand the issue;)
Does anyone have an idea of what may be going on?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Zaha Zorg 所说:默认情况下,Django 的 Cookie 不适用于前置 www 和非 www 域。
然而,这里更深层次的问题是您允许网站的 www 和非 www 域提供相同的内容。除了两者之间的流量分配带来的明显的 SEO 后果外,您还会遇到此类问题。处理此问题的正确方法是将所有流量从一个重定向到另一个(无论您喜欢哪种)。您发现的
PREPEND_WWW
设置非常适合此操作。对于相反的情况(强制所有流量到非 www),建议仅在服务器配置级别进行重写,例如 Apache 或 Nginx。What Zaha Zorg said: Cookies from Django won't work for both a prepended www and non-www domain by default.
However, the deeper issue here is that you're allowing both www and non-www domains of your site to serve identical content. Besides the obvious SEO consequences of having traffic split between the two, you run into issues like these. The proper way to handle this is to redirect all traffic from one to the other (whichever you prefer). The
PREPEND_WWW
setting you found works perfectly for this. For the opposite (forcing all traffic to non-www), it's recommended to just do a re-write at the server configuration level, such as Apache or Nginx.您需要查看 https://docs .djangoproject.com/en/dev/ref/settings/?from=olddocs#session-cookie-domain
You need to look at https://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#session-cookie-domain
难道 cookies 依赖于服务器的主机名吗?这可以解释为什么两个域名被认为是不同的。
Could it be that cookies depend on the hostname of the server ? This could explain why both domain names are considered different.