my8100 / scrapyd-cluster-on-heroku

Set up free and scalable Scrapyd cluster for distributed web-crawling with just a few clicks. DEMO :point_right:
https://scrapydweb.herokuapp.com/
GNU General Public License v3.0
122 stars 94 forks source link

The ScrapydWeb app is redirected to HTTP #2

Closed leffss closed 5 years ago

leffss commented 5 years ago

为什么我的 heroku 应用访问 https 时,会 302 跳转到 http,是 heroku 的原因,还是 scrapydweb 的原因呢?

捕获

my8100 commented 5 years ago

It's an issue of Heroku: https://help.heroku.com/J2R1S4T8/can-heroku-force-an-application-to-use-ssl-tls And I am not gonna fix it for the time being as it does not affect the use of ScrapydWeb.

my8100 commented 5 years ago

https://github.com/my8100/scrapydweb/blob/a0bcf07b9fdd079ba97596871fe810aa512e6b2f/scrapydweb/__init__.py#L77

    @app.route('/hello')
    def hello():
        return 'Hello, World!'

Add the code block below to work around this issue.

    # https://stackoverflow.com/questions/15116312/redirect-http-to-https-on-flaskheroku
    # On Heroku, SSL (https) is terminated before it reaches your application,
    # so you app never actually sees SSL traffic.
    # request.url would always start with 'http://' even if
    # you visit https://scrapydweb.herokuapp.com
    # https://help.heroku.com/J2R1S4T8/can-heroku-force-an-application-to-use-ssl-tls
    @app.after_request
    def after_request(response):
        from flask import request
        from six.moves.urllib.parse import urljoin
        if (request.headers.get('X-Forwarded-Proto') == 'https'
            and 'Location' in response.headers):
            # Location: /1/servers/
            response.headers['Location'] = re.sub(r'^http://', 'https://',
                                                  urljoin(request.url, response.headers['Location']))
        return response
leffss commented 5 years ago

thinks