Django:安全中间件使站点崩溃

标签 django ssl nginx django-settings django-middleware

在生产环境中,我一直在尝试添加 djangosecure.middleware.SecurityMiddleware(来自 http://pypi.python.org/pypi/django-secure)to 我的设置,但没有成功。

当我运行时:

./manage.py checksecure

一切顺利。但我无法加载网站。它给了我以下错误:

The webpage has resulted in too many redirects. Clearing your cookies for this site or 
allowing third-party cookies may fix the problem. If not, it is possibly a server 
configuration issue and not a problem with your computer.

在本地,当我使用生产设置时,我收到一个页面错误:

Unable to make a secure connection to the server. This may be a problem with the server,
or it may be requiring a client authentication certificate that you don't have.

然后我的终端充满了我无法破译的奇怪错误:

[12/Jan/2013 14:15:25] code 400, message Bad HTTP/0.9 request type    
('\x16\x03\x01\x00\x98\x01\x00\x00\x94\x03\x02P\xf1\xc4]\x97e\xdd\xdc\xa9\xeb\x0e\xfc\xbb\xfa3 ')
[12/Jan/2013 14:15:25] "??P??]?e?ܩ????3 Ʀ?-?:?.E:?o?FH?" 400 -
[12/Jan/2013 14:15:25] code 400, message Bad request syntax     ('\x16\x03\x01\x00\x98\x01\x00\x00\x94\x03\x02P\xf1\xc4]M\xeeA50\xfc\x15%\xc1\xa4\x02\xec\xf0\x1fO')
[12/Jan/2013 14:15:25] "??P??]M?A50?%????O" 400 -
[12/Jan/2013 14:15:25] code 400, message Bad request syntax ('\x16\x03\x01\x00\x98\x01\x00\x00\x94\x03\x01P\xf1\xc4]\x8eg\xbey\x155\xafiP5\x85r\xb4|\x8c\x

有什么建议吗?

最佳答案

无限重定向意味着你设置了SECURE_SSL_REDIRECTTrue,但在生产环境中,您的站点在 SSL 剥离代理服务器后面运行,因此 Django 无法判断该请求实际上已经是 SSL,因此它会不断尝试重定向到 SSL。如链接文档中所述,您需要找出代理设置的 header 以指示外部 SSL 请求,并设置 SECURE_PROXY_SSL_HEADER相应地设置。

在本地使用生产设置将不起作用,因为 Django 的开发服务器不支持 SSL。奇怪的终端输出是您的浏览器试图与不理解 SSL 的服务器进行 SSL 握手。

关于Django:安全中间件使站点崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297691/

相关文章:

python - 自定义 django admin ChangeForm 模板/添加自定义内容

ssl - 有没有办法让所有浏览器都忽略自签名证书?

ruby-on-rails - 不同环境的不同nginx配置

Django 1.5.4 - sqlite 日期时间 - model.datetimeField 返回 None

python - Django 背景图像 CSS

c# - 使用签名证书设置 ssl 服务器套接字

java - keystore 类型 : which one to use?

mysql - 使用 nginx 和两个物理 Linux 服务器实现 Web 应用程序的可扩展性

nginx - GitLab-workhorse 的套接字去了哪里?

django - robots.txt 的 URL 中的尾部斜杠