Pyramid调试工具栏通过HTTP而不是HTTPS提供静态内容

时间:2016-08-19 07:12:54

标签: python python-3.x pyramid pylons pyramid-debug-toolbar

在我们的测试服务器上,我们正在使用Pyramid debug toolbar,但是,它会生成http://指向静态内容的链接(如CSS和JavaScript文件),而其他内容则是通过HTTPS提供服务。这会导致混合内容警告,并且会破坏所有功能。有没有办法强制它生成HTTPS链接?

我知道可以在Chrome中启用混合内容,这样可行,但对整个QA团队来说,这不是一个可行的解决方案。

3 个答案:

答案 0 :(得分:4)

可能有更好/更简单的方法来实现这一目标,但您可以做的一件事就是为_scheme='https'的每次调用添加request.static_url()参数。

为此您可以编辑pyramid/url.py,但您也可以在项目__init__.py中执行此操作:

from pyramid.url import URLMethodsMixin

URLMethodsMixin.static_url_org = URLMethodsMixin.static_url  # backup of original

def https_static_url(self, *args, **kw):
    kw['_scheme'] = 'https'  # add parameter forcing https
    return URLMethodsMixin.static_url_org(self, *args, **kw)  # call backup

URLMethodsMixin.static_url = https_static_url  # replace original with backup

static_url的参数与route_url类似。来自文档:

  

请注意,如果_scheme作为https传递,并且未传递_port,则假定_port值已作为443传递。同样,如果_scheme作为http传递且未传递_port,则假定_port值为已经传递为80.为了避免这种行为,每当你传递_scheme时,总是显式传递_port。   设置'_scheme'会自动强制移植端口443

答案 1 :(得分:1)

通常,您通过传递X-Forwarded-Proto HTTP标头来通知您的Web服务器使用HTTPS而不是HTTP。

来自Nginx的例子:

    proxy_set_header X-Forwarded-Proto $scheme;

但是,这不是标准的,可能取决于您的Web服务器配置。以下是Nginx + uWSGI的完整示例:

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Forwarded-Proto $scheme;

    uwsgi_pass 127.0.0.1:8001;
    uwsgi_param UWSGI_SCHEME https;
    uwsgi_pass_header X_FORWARDED_PROTO;
    uwsgi_pass_header X_REAL_IP;

See how WebOb (underlying Request for Pyramid) reconstructs URL from given HTTP headers

答案 2 :(得分:0)

https://stackoverflow.com/a/42358816/358532

您可以将url_scheme参数添加到配置文件中(以 环境):

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6500
url_scheme = https