2016-08-19 28 views
2

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

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

回答

2

可能有更好/更简单的方法来达到此目的,但您可以做的一件事就是将_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

通常你显示你的Web服务器穿过X-Forwarded-Proto HTTP报头使用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