如何强制对我的 Django 应用程序的某些 URL 使用 SSL? [英] How to force the use of SSL for some URL of my Django Application?

查看:29
本文介绍了如何强制对我的 Django 应用程序的某些 URL 使用 SSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定我网站的某些 URL 将使用 SSL.我已经在 SO 上看到了很多答案.

I want to be sure that for some URL of my website, SSL will be use. I saw a lot of answer already on SO.

强制重定向到 SSL 以外的所有页面一个

所以我想我会使用mod_rewrite.

我的问题更多是关于如何配置虚拟主机以通过HTTPHTTPS运行我的Django应用程序代码> 没有问题.我正在使用 WSGI.

My question is more about how to configure the Virtual Host to run my Django Application over HTTP and over HTTPS without problems. I am using WSGI.

只是在 *:443*:80 上复制配置有问题吗?我应该怎么做才能获得最佳配置?

Is it a problem to just duplicate the configuration over *:443 and over *:80? What should I do to have the best configuration?

推荐答案

如果 WSGI 实际上是指 Apache/mod_wsgi,那么尽管挂载的 WSGI 应用程序通常会在它们自己的子解释器中运行,但 80/443 拆分是一个特例并且即使在不同的VirtualHost中,只要WSGIScriptAlias的挂载点和ServerName相同,它们就会合并.

If by WSGI you actually mean Apache/mod_wsgi, then although mounted WSGI applications normally get run in their own sub interpreters, the 80/443 split is a special case and even though in different VirtualHost so long as mount point for WSGIScriptAlias, and the ServerName are the same, they will be merged.

<VirtualHost *:80>
ServerName www.example.com

WSGIScriptAlias / /some/path/django.wsgi.
</VirtualHost>

<VirtualHost *:443>
ServerName www.example.com

WSGIScriptAlias / /some/path/django.wsgi.
</VirtualHost>

守护进程模式也会发生这种情况,但是在守护进程模式下,您只需要在第一个 VirtualHost 定义中定义一个守护进程组,然后使用 WSGIProcessGroup 从两者中引用即可.

This will happen for daemon mode as well, but with daemon mode you need to define only a single daemon process group in first VirtualHost definition and then just refer to that from both with WSGIProcessGroup.

<VirtualHost *:80>
ServerName www.example.com

WSGIDaemonProcess mydjangosite ...
WSGIProcessGroup mydjangosite

WSGIScriptAlias / /some/path/django.wsgi.
</VirtualHost>

<VirtualHost *:444>
ServerName www.example.com

WSGIProcessGroup mydjangosite

WSGIScriptAlias / /some/path/django.wsgi.
</VirtualHost>

WSGIProcessGroup 只能通过相同的 ServerName 到达 VirtualHost.

The WSGIProcessGroup can only reach across like to that VirtualHost for same ServerName.

Django 提供了一个 is_secure() 方法,用于确定请求何时通过 HTTPS 发送,该方法源自 WSGI 变量,请求名为wsgi.url_scheme",由 mod_wsgi 设置.

Django provides a is_secure() method for determining when request came via HTTPS which derives from WSGI variable with request called 'wsgi.url_scheme' which is set by mod_wsgi.

因此,您将拥有一个 Django WSGI 脚本文件和设置文件.您只需要按照 Apache/mod_wsgi 配置中的描述复制应用程序安装.

So, you would have one single Django WSGI script file and settings file. You just need to duplicate application mounting as decsribed in Apache/mod_wsgi configuration.

这篇关于如何强制对我的 Django 应用程序的某些 URL 使用 SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆