金字塔调试工具栏通过HTTP而不是HTTPS提供静态内容。

3
在我们的测试服务器上,我们使用Pyramid debug toolbar,然而它生成的链接到静态内容(如CSS和JavaScript文件)都是http://,而其他内容则通过HTTPS传输。这会导致混合内容警告,并且破坏所有功能。有没有一种方法可以强制它生成HTTPS链接?
我知道在Chrome中启用混合内容是可能的,这有效,但对于整个QA团队来说并不可行。
3个回答

5

可能有更好/更简单的方法来实现这个目标,但是你可以在每次调用request.static_url()时添加_scheme='https'参数来实现这个目标。

当然,你可以编辑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。

2
通常情况下,您可以通过传递 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;

点击查看WebOb(Pyramid的基础请求)如何从给定的HTTP头重构URL


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接