简体   繁体   English

如何使用django-sslify在我的Django + nginx + gunicorn网络应用程序上强制https,并依赖Cloudflare的新免费SSL?

[英]How to use django-sslify to force https on my Django+nginx+gunicorn web app, and rely on Cloudflare's new free SSL?

Intro 介绍

Cloudflare's providing SSL for free now, and I would be a fool to not take advantage of this on my site, and a downright dickhead to break everything in the process of trying to. Cloudflare现在免费提供SSL ,如果我不能在我的网站上利用这一点,那将是一个愚蠢的行为,并且在尝试过程中打破一切的彻头彻尾的傻瓜。

I can code apps just fine, but when it comes to setting up or configuring https/nginx/gunicorn/etc/idon'tknowtheterminology, I know barely enough to follow Googled instructions. 我可以很好地编写应用程序代码,但是当涉及到设置或配置https / nginx / gunicorn / etc / idon'tknowtheminminology时,我几乎无法遵循Googled说明。

Question

I would like to use django-sslify to force https on my Django web app. 我想使用django-sslify在我的Django网络应用程序上强制https。 How may I achieve this without upsetting the balance in my life , given the following known facts? 考虑到以下已知事实,如何 在不破坏生活平衡的情况下 实现这一目标?

Known facts 已知事实

  1. I'm using Django 1.7, running on a DigitalOcean server hooked up to a (free) Cloudflare DNS. 我正在使用Django 1.7,在连接到(免费)Cloudflare DNS的DigitalOcean服务器上运行。 Django is fitted (served?) with nginx and gunicorn. Django适合(服务?)与nginx和gunicorn。 Basically followed this guide to get it all set up. 基本上遵循本指南来完成所有设置。
  2. Accessing my website currently defaults to a regular http://example.com header. 访问我的网站目前默认为常规的http://example.com标题。
  3. Manually accessing https://example.com works with the green lock and all, but this breaks all form submissions with the error "(403) CSRF verification failed. Request aborted.". 手动访问https://example.com使用绿色锁和所有,但这会中断所有表单提交错误“(403)CSRF验证失败。请求中止。”。
  4. In my Cloudflare site settings, the domain is currently configured to "Flexible SSL". 在我的Cloudflare网站设置中,域当前配置为“灵活SSL”。
  5. Trying to use django-sslify with my existing setup totally breaks everything, and the browser is unable to return a response. 尝试在我现有的设置中使用django-sslify完全打破了一切,浏览器无法返回响应。
  6. This info nugget tells me that I should use the "Full SSL" configuration setting when using django-sslify with Cloudflare's SSL. 这个信息块告诉我,当使用django-sslify和Cloudflare的SSL时,我应该使用“Full SSL”配置设置。
  7. Cause for hesitation found here where it is mentioned that a $20/mo Pro Cloudflare account is needed to handle SSL termination. 这里有犹豫的原因,提到需要20美元/月的Pro Cloudflare帐户来处理SSL终止。 So I really don't want to screw this up :/ 所以我真的不想搞砸了:/
  8. There was only 1 mention of "http" or "https" anywhere in my nginx and gunicorn configuration, specifically in my nginx config: 在我的nginx和gunicorn配置中,只有一个提到“http”或“https”,特别是在我的nginx配置中:

location / { proxy_pass http://127.0.0.1:8001; ... } location / { proxy_pass http://127.0.0.1:8001; ... } proxy_pass http://127.0.0.1:8001; ... }

Ok I think that's all I have 好吧,我认为这就是我的全部

Also, my server is providing an Django Rest Framework api for a Phonegap app, does that need to be taken in to consideration? 此外,我的服务器为Phonegap应用程序提供Django Rest Framework api,需要考虑吗? If I need to provide addtional information do let me know and I'll get back to you. 如果我需要提供附加信息,请告诉我,我会回复您。 Thank you for taking a look at this! 谢谢你看看这个! :) :)

CloudFlare allows you to enable specific page rules , one of which is to force SSL (by doing a hard redirect ). CloudFlare允许您启用特定页面规则 ,其中之一是强制SSL(通过执行硬重定向 )。 This is a great thing to use in addition to django-sslify or django-secure 除了 django-sslifydjango-secure 之外,这是一件很棒的事情


In addition to setting up your SSL redirect, you also need to tell Django to handle secure requests. 除了设置SSL重定向之外,您还需要告诉Django处理安全请求。 Luckily, Django provides a decent guide for doing this, but there are a few things that it doesn't mention but I've had to do with nginx. 幸运的是, Django为这样做提供了一个不错的指南 ,但有一些事情没有提及,但我与nginx有关。

In your Django settings, you need to tell Django how to detect a secure request 在Django设置中,您需要告诉Django如何检测安全请求

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')

In your nginx configuration you need to set up the X-Forwarded-Protocol header (and the X-Forwarded-For / X-Scheme headers are also useful). 在您的nginx配置中,您需要设置X-Forwarded-Protocol标头(并且X-Forwarded-For / X-Scheme标头也很有用)。

proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

You also need to proxy the Host header down, so Django is able to read the correct host and port, which is used in generating absolute urls and CSRF, among other things. 您还需要向下代理Host头,因此Django能够读取正确的主机和端口,用于生成绝对URL和CSRF等。

proxy_set_header Host $http_host;

Note that I used the $http_host variable instead of $host or $host:$server_port . 请注意,我使用$http_host变量而不是$host$host:$server_port This will ensure that Django will still respect CSRF requests on non-standard ports, while still giving you the correct absolute urls. 这将确保Django仍然会在非标准端口上遵守CSRF请求,同时仍然为您提供正确的绝对URL。

As with most things related to nginx and gunicorn, YMMV and it gets easier after you do it a few times. 与大多数与nginx和gunicorn相关的事情一样,YMMV在你做了几次之后会变得更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM