简体   繁体   English

如何确定我的mod_wsgi应用程序是否正在HTTPS上运行?

[英]How do I determine if my mod_wsgi application is running on HTTPS?

I've just inherited a Python application which is running under Apache 2.4, mod_wsgi 3.4 and Python 2.7. 我刚刚继承了一个在Apache 2.4,mod_wsgi 3.4和Python 2.7下运行的Python应用程序。 The same application serves both HTTP and HTTPS requests. 相同的应用程序可同时满足HTTP和HTTPS请求。

In the existing code, it is trying to determine if the reqeust was HTTPS by checking the environment: 在现有代码中,它通过检查环境来尝试确定请求是否为HTTPS:

if context.environ.get('HTTPS') not in ['on', '1']:

This check is failing, even when the connection actually was HTTPS. 即使连接实际上是HTTPS,此检查也失败。 On looking at an extended traceback showing the environment variables, I saw that HTTPS was not actually in the environment passed from Apache. 在查看显示环境变量的扩展追溯时,我看到HTTPS实际上不在从Apache传递的环境中。

So my questions are: 所以我的问题是:

  • Is this an Apache configuration problem? 这是Apache配置问题吗?
  • Is this check completely wrong and should be rewritten to check something else? 此检查是否完全错误,应重写以检查其他内容? And if so, what? 如果是这样,那又如何呢?
  • Or should I give up and replace Apache with nginx like I really want to do? 还是应该像我真正想要的那样放弃并用nginx替换Apache?

Apache's mod_ssl can be configured to set the HTTPS environment variable, but doesn't do so by default for performance reasons. 可以将Apache的mod_ssl配置为设置HTTPS环境变量,但是出于性能原因,默认情况下不这样做

You could explicitly enable it, but since you're using a WSGI application, it's probably a better idea to check the wsgi.url_scheme environment variable instead; 您可以显式启用它,但由于使用的是WSGI应用程序,因此最好检查一下wsgi.url_scheme环境变量; the WSGI spec guarantees its presence, and it won't require any further changes to your application if you do eventually move to nginx. WSGI规范保证了它的存在,并且如果您最终移至nginx,则它不需要对应用程序进行任何进一步的更改。

When using CGI, WSGI or SSI you will need to advise Apache to send the HTTPS header otherwise it will beempty. 使用CGI,WSGI或SSI时,您需要建议Apache发送HTTPS标头,否则它将为空。 You can do this with mod_env in the config or .htaccess 您可以使用config或.htaccess中的mod_env来完成此操作

<IfModule mod_env.c>
   SetEnv HTTPS on
</IfModule>

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

相关问题 如何从 mod_wsgi 优雅地关闭应用程序 - How to do graceful application shutdown from mod_wsgi 如何在Ubuntu的xampp服务器上安装mod_wsgi? 遇到libtool错误 - How do I install mod_wsgi to xampp server on Ubuntu? Running into libtool errors 如何在Mac(High Sierra)上安装mod_wsgi? - How do I install mod_wsgi on Mac (High Sierra)? 如何调试重新启动mod_wsgi进程的apache? - How do I debug apache restarting a mod_wsgi process? 如何在Apache和mod_wsgi中使用Flask路由? - How do I use Flask routes with Apache and mod_wsgi? 如何在mod_wsgi中使用conda环境? - How do I use a conda environment with mod_wsgi? django是否已经附带了wsgi应用程序文件,还是需要创建一个? 如何使用django设置apache和mod_wsgi? - Does django already come with a wsgi application file, or do I need to create one? How to setup apache and mod_wsgi with django? Wamp 没有与 mod_wsgi 一起运行? - Wamp not running with mod_wsgi? 如何使用mod_wsgi实现mod_python风格的authenhandler / authzhandler? - How do I implement a mod_python style authenhandler/authzhandler using mod_wsgi? 当我将其与Python 3 mod_wsgi应用程序一起使用时,为什么粘贴ErrorMiddleware会引发异常? - Why does Paste ErrorMiddleware throw an exception when I use it with my Python 3 mod_wsgi application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM