Reputation: 1275
i want to serve static files in pyramids via request.static_url('some_file'). Due to several services, my templates got lines like:
<script type="text/javascript" src="${request.static_url('dbas:static/first')}"></script>
<script type="text/javascript" src="${request.static_url('websocket:static/second')}"></script>
But unfortunately the method static_url() only delivers links with http as url_scheme, but i want https. How can I achieve this?
Thanks!
Upvotes: 3
Views: 1272
Reputation: 41
I know this question is quite old but since I struggled a bit with that one, I thought I could give some info.
In the scenario where your have
1-A reverse proxy -> 2-A service running the pyramid App -> 3-The pyramid app code
3- Pyramid generates the url using the request.environ['wsgi.url_scheme']
2- The service running the app should set the wsgi_url_scheme. Gunicorn for example set this attribute if the reverse proxy set the X-Forwarded-Proto and if the reverse proxy ip is listed in the forwarded_allow_ips
1- The reverse proxy should set the X-Forwarded-Proto header
In the case of gunicorn + nginx
For nginx set the header add the following in the SSL part
proxy_set_header X-Forwarded-Proto https;
For gunicorn the IP address of the nginx reverse proxy should be specified in the command line as described in the docs
gunicorn -b 0.0.0.0:8080 --workers=4 --timeout=60 --paster /config/myapp.ini --forwarded-allow-ips 1.2.3.4
Upvotes: 0
Reputation: 81
You can add url_scheme param to your configuration file (separated by environment) like that:
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6500
url_scheme = https
Upvotes: 6
Reputation: 2341
Easy, you only need to specify the scheme you want, for example:
<script type="text/javascript" src="${request.static_url('dbas:static/first', _scheme='https')}"></script>
Note: You can also specify _host or _port to define the url. For more info http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.route_url
Upvotes: 1