It would be nice if requests to HTTP could automatically redirect to HTTPS. May you know how to do it?

658 views
Skip to first unread message

Maxim Khrichtchatyi

unread,
Aug 20, 2013, 4:45:47 AM8/20/13
to python-...@googlegroups.com
Help

Felinx Lee

unread,
Aug 20, 2013, 4:50:22 AM8/20/13
to python-...@googlegroups.com
You can use rewrite rule in HTTP server to do that.

For example(nginx)

server {
    listen 80;
    server_name www.example.com;

    location / {
         rewrite ^(.*)$ https://www.example.com$1 permanent;
    }
}


On Tue, Aug 20, 2013 at 4:45 PM, Maxim Khrichtchatyi <maxim.khr...@gmail.com> wrote:
Help

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
What can change the nature of a man?(Planescape Torment)
----------------------------------------------------------------------------------------
http://feilong.me            Felinx Lee's Blog (Chinese Only)

Jacek Artymiak

unread,
Aug 20, 2013, 4:50:33 AM8/20/13
to python-...@googlegroups.com
Have a look at RequestHandler.redirect(url, permanent=False)

Maxim Khrichtchatyi

unread,
Aug 20, 2013, 4:58:32 AM8/20/13
to python-...@googlegroups.com
don't want to use nginx)

Jacek Artymiak

unread,
Aug 20, 2013, 5:00:16 AM8/20/13
to python-...@googlegroups.com
You can also do it on the firewall

Felinx Lee

unread,
Aug 20, 2013, 6:35:51 AM8/20/13
to python-...@googlegroups.com

You can override prepare to redirect to https like below:


class MainHandler(tornado.web.RequestHandler):

    def prepare(self):

        if self.request.protocol == "http":

            self.redirect("https://%s" % self.request.full_url()[len("http://"):],

                          permanent=True)


    def get(self):

        self.write("Hello, world")

Maxim Khrichtchatyi

unread,
Aug 20, 2013, 8:24:00 AM8/20/13
to python-...@googlegroups.com
http://IP_ADDRESS:4443/

WARNING:tornado.general:SSL Error on 8 ('IP_ADDRESS', 51453): [SSL: HTTP_REQUEST] http request (_ssl.c:547)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import tornado.ioloop
import tornado.web
import tornado.httpserver
import http.server


class MainHandler(tornado.web.RequestHandler):
    def prepare(self):
        if self.request.protocol == "http":
            self.redirect("https://%s" % self.request.full_url()[len("http://"):], permanent=True)

    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r'/', MainHandler),
])

http_server = tornado.httpserver.HTTPServer(application,
    ssl_options = {
    "certfile": os.path.join("/var/pyTest/keys/", "cert.pem"),
    "keyfile": os.path.join("/var/pyTest/keys/", "key.pem"),
    }
)

if __name__ == '__main__':
    http_server.listen(4443)
    tornado.ioloop.IOLoop.instance().start()

Ben Darnell

unread,
Aug 20, 2013, 9:08:57 AM8/20/13
to Tornado Mailing List
It's not really possible to detect and redirect HTTP requests *made to the HTTPS port*.  See the discussion in 

-Ben
Reply all
Reply to author
Forward
0 new messages