【问题标题】:Django Project can't force Google Appengine to redirect to httpsDjango Project 无法强制 Google Appengine 重定向到 https
【发布时间】:2017-05-25 17:42:13
【问题描述】:

使用示例 Django 项目和我的 Django Rest Framework 项目,我可以毫无问题地部署到应用引擎。

我可以通过https://myappnamehere.appspot.com 和http:// 版本访问该站点。

但是,我似乎无法强制它只允许 HTTPS。

尝试 1: 在我的 Django 设置中,我尝试设置:

SECURE_SSL_REDIRECT = True

最后我的项目不再显示在应用引擎上,AppEngine 报告我应该在 30 分钟后重试

尝试 2: 在 app.yaml 中,我遵循 here 的建议 和其他堆栈溢出线程通过添加这个:

handlers:
- url: /*
  script: myapplication.wsgi.application

这也导致路由看起来很混乱,我所有的 URL 都不再像预期的那样通过 django 路由器路由。

wsgi 里面有什么: 导入操作系统

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapplication.settings.settings")
application = get_wsgi_application()

尝试 3:抱歉,我忘了提及我一直尝试使用安全功能,结果我的网站也无法再次加载。

handlers:
- url: /*
  script: myapplication.wsgi.application
  secure: always

【问题讨论】:

    标签: python django google-app-engine django-rest-framework


    【解决方案1】:

    只需在 app.yaml 文件中添加一个安全参数即可。

    handlers:
    - url: /*
      script: anyfile.py
      secure: always
    

    See Configuring Secure URLs in app.yaml

    【讨论】:

    • 对不起,我也试过了。相反,我的网站根本无法加载。
    • 您的 SSL 证书是否涵盖裸域以及 www 子域?你都试过了吗?
    • @GAEfan,我什至不再为 SSL 尝试自定义域,即使将其部署在 appengine 实例上也不起作用。设置 Django 设置文件和 app.yaml 似乎没有做任何事情,只是导致站点报告“错误:服务器错误您请求的服务尚不可用。请在 30 秒后重试。”
    • app.yaml 中正确的语法是- url: /.*。您将需要在云控制台中检查您的日志,以查看 500 错误的堆栈跟踪。它应该会给你一个提示。
    【解决方案2】:

    正如 Bravin 所说,一个简单的方法是将secure: always 添加到app.yaml。但是,如果您关心一致的子域(例如,总是转到www. 地址),那么您可能需要编写自己的中间件来重定向到'https://www....`

    一致的子域是 SEO 的事情。搜索引擎可以将裸域和www. 域视为不同的地址。此外,一些 SSL 证书仅涵盖一个子域(即www.),而不是裸域。

    如果您编写自己的中间件,请确保免除任务、cron、后端等,否则它们可能会在返回 301 时卡住。还可以免除您的 localhost 开发请求。

    此外,仍有一小部分使用旧版浏览器或操作系统的用户无法使用 SNI 协议提供 SSL。你用这些做什么?在此示例中,我们仍然使用 appspot.com 证书为他们提供安全内容。

    示例中间件:

    from django.http import HttpResponsePermanentRedirect
    import os
    import logging
    
    class ForceHttps(object):
        '''
        We want all requests to go to https://www.{mysite}.com
        except: Cron, Taskqueue, backend jobs, dev server
    
        test this against secure: always in app.yaml
    
        In this example, we redirect non-SNI compatible browsers to the secure appspot.com address
        '''
    
        def process_request(self, request):
    
            user_agent = request.META.get('HTTP_USER_AGENT', 'fake')
    
            if (    'AppEngine-Google' in user_agent or 
                    'mybackendmodule' in request.META.get('CURRENT_MODULE_ID') or
                    'dot-appname' in request.META.get('HTTP_HOST') or
                     os.environ.get('SERVER_SOFTWARE', '').lower().startswith('devel') ):
                return None
    
            # for non-SNI SSL browsers, we send to appspot domain:
            if (
                    ((('Windows NT 5.1' in user_agent) or ('Windows XP' in user_agent)) and (('MSIE' in user_agent) or ('Safari' in user_agent) or ('Chrome' in user_agent))) or        # XP with most browsers
                    (('MSIE 6' in user_agent) or ('MSIE 5' in user_agent)) or                                                 # any version of IE6 or 5
                    ((('Windows NT 6.1' in user_agent) or ('Windows NT 6.2' in user_agent)) and ('webDAV' in user_agent)) or  # IE7 or 8 with webDAV
                    (('Android 2.' in user_agent) or ('Android 1.' in user_agent)) ):                                         # android 2.x
    
                logging.info('Redirecting to appspot.  SNI incompatibility detected: ' + user_agent )
    
                return HttpResponsePermanentRedirect("https://{appname}.appspot.com" + request.META.get('PATH_INFO'))
    
    
    
            # for SNI compatible browsers:
            if request.META.get('HTTPS') == 'off' or 'www' not in request.META.get('HTTP_HOST') :
                return HttpResponsePermanentRedirect("https://www.{mysite}.com" + request.META.get('PATH_INFO'))
    
            return None
    

    请务必在SETTINGS.py 中添加'path_to.my_middleware.ForceHttps'MIDDLEWARE_CLASSES

    【讨论】:

      【解决方案3】:

      这里的所有答案都帮助我走上了正确的道路。这是经过一百万次尝试后适用于我的项目的最终 app.yaml 设置。 (我还试图通过限制资源来降低 appengine 成本

      # [START runtime]
      vm: true
      runtime: custom
      service: backend-dev
      
      manual_scaling:
         instances: 1
      
      resources:
        cpu: .5
        memory_gb: 0.6
        disk_size_gb: 10
      
      handlers:
      
      - url: /static
        static_dir: static
      
      - url: /.*
        script: myapplication.wsgi.py
        secure: always
      
      # [END runtime]
      

      【讨论】:

        猜你喜欢
        • 2016-11-14
        • 2020-05-28
        • 1970-01-01
        • 1970-01-01
        • 2019-03-24
        • 2020-07-23
        • 2016-10-14
        • 2021-01-01
        • 2011-11-03
        相关资源
        最近更新 更多