Skip to content

Instantly share code, notes, and snippets.

@blbradley
Created November 6, 2014 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blbradley/e9564f6a0c9a1d6c2245 to your computer and use it in GitHub Desktop.
Save blbradley/e9564f6a0c9a1d6c2245 to your computer and use it in GitHub Desktop.
WebOb middleware to force ssl on Heroku
import os
from webob.dec import wsgify
from webob.exc import HTTPMovedPermanently
from paste.deploy import loadapp
from waitress import serve
@wsgify.middleware
def ForceSSLMiddleware(req, app):
# check that the incoming request was using https
if req.headers['X-Forwarded-Proto'] != 'https':
https_url = req.url.replace('http://', 'https://')
return HTTPMovedPermanently(location=https_url)
# override the request scheme to tell the app that we are using https
req.scheme = 'https'
resp = req.get_response(app)
return resp
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:production.ini', relative_to='.')
app = ForceSSLMiddleware(app)
serve(app, host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment