因爲我花了幾個小時通過涉水掛架/路由/燒杯中的/ etc。來源我想我會分享我的解決方案。
先介紹一點情況。我在AWS上使用SSL終止的彈性負載均衡器(ELB)。該應用程序構建爲僅通過https運行;畢竟,這是一個火燒後的世界。它的分層像這樣:
ELB -> nginx -> pasteWSGI -> pylons
ELB是簡單性方面,而是pylons.controllers.util.redirect任何呼叫快活好將觸發302重定向到的「http:// mysite的/」。 ELB在返回時不會改變(無理由),因此我的瀏覽器將被髮送回80端口,並且在該端口上沒有ELB監聽。
我試着按上面建議的更新Mapper。
- 沒有工作,
- 我想我的重定向是相對的。切換到掛機中的https意味着URL生成器會提取並提取主機以創建新的URL(https:// localhost/....)
請注意,Mapper.redirect_to可用於開箱即用相對重定向,所以沒有必要搞砸了。根本問題是controllers.redirect使用稍微不同的代碼路徑。特別是在Routes中,controllers.util.redirect不是一個重定向(有一個「if routes and routes.redirect」,它將轉化爲False)。
我的解決方案:用新的控制器方法(也稱爲重定向)替換所有調用重定向以將重定向從絕對重定向更改爲相對重定向。
代碼如下:
lib/helpers。PY
def relative_redirect(to_url, start_response):
"""Returns a redirect that is compatible with AWS ELB (no absolute http responses)
Using pylons.controllers.util.redirect triggers an exception that'll be turned into a 302
But with an absolute path so the response does not contains https but simple http
"""
start_response("302 Found", [("Content-Type", "text/plain; charset=utf-8"), ("Location", url(to_url))])
return ["You are being redirected to {0}".format(url(to_url))]
與該位從基類我控制器稱爲:
class BaseController(WSGIController):
...
def redirect(self, to_url):
"""Force a relative redirection, to work with AWS ELB"""
return relative_redirect(to_url, self.start_response)