【问题标题】:Daphne on Elastic beanstalk - WebSocket handshake: Unexpected response code: 500弹性豆茎上的达芙妮 - WebSocket 握手:意外的响应代码:500
【发布时间】:2020-10-13 14:30:56
【问题描述】:

按照本指南 https://medium.com/@elspanishgeek/how-to-deploy-django-channels-2-x-on-aws-elastic-beanstalk-8621771d4ff0 设置使用 websockets 的 django 应用程序。

01_env.config

option_settings:  
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: dashboard.settings
    PYTHONPATH: /opt/python/current/app/dashboard:$PYTHONPATH
  aws:elasticbeanstalk:container:python:
    WSGIPath: dashboard/wsgi.py

  aws:elbv2:listener:80:
    DefaultProcess: http
    ListenerEnabled: 'true'
    Protocol: HTTP
    Rules: ws
  aws:elbv2:listenerrule:ws:
    PathPatterns: /websockets/*
    Process: websocket
    Priority: 1
  aws:elasticbeanstalk:environment:process:http:
    Port: '80'
    Protocol: HTTP
  aws:elasticbeanstalk:environment:process:websocket:
    Port: '5000'
    Protocol: HTTP

02_setup.config

container_commands:
  00_pip_upgrade:
    command: "source /opt/python/run/venv/bin/activate && pip install --upgrade pip"
    ignoreErrors: false
  01_migrate:
    command: "django-admin.py migrate"
    leader_only: true
  02_collectstatic:
    command: "django-admin.py collectstatic --noinput"
  03_wsgipass:
    command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
  04_celery_tasks:
    command: "cat .ebextensions/celery_configuration.txt > /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh && chmod 744 /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh"
    leader_only: true
  05_celery_tasks_run:
    command: "/opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh"
    leader_only: true

03_proxy.config

files:
  "/etc/httpd/conf.d/proxy.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      ProxyPass /websockets/ ws://127.0.0.1:5000/websockets/
      ProxyPassReverse /websockets/ ws://127.0.0.1:5000/websockets/

打开 websocket 时,我在 chrome 的控制台上收到消息:

WebSocket connection to 'ws://xx.xx.xx.elasticbeanstalk.com/websockets/' failed: Error during WebSocket handshake: Unexpected response code: 500

日志显示:

AH01144: No protocol handler was valid for the URL /websockets/ (scheme 'ws'). If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

通过运行 sudo apachectl -M 加载了 EC2 实例上列出的 apache 模块:

Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 access_compat_module (shared)
 actions_module (shared)
 alias_module (shared)
 allowmethods_module (shared)
 auth_basic_module (shared)
 auth_digest_module (shared)
 authn_anon_module (shared)
 authn_core_module (shared)
 authn_dbd_module (shared)
 authn_dbm_module (shared)
 authn_file_module (shared)
 authn_socache_module (shared)
 authz_core_module (shared)
 authz_dbd_module (shared)
 authz_dbm_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_owner_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cache_module (shared)
 cache_disk_module (shared)
 cache_socache_module (shared)
 data_module (shared)
 dbd_module (shared)
 deflate_module (shared)
 dir_module (shared)
 dumpio_module (shared)
 echo_module (shared)
 env_module (shared)
 expires_module (shared)
 ext_filter_module (shared)
 filter_module (shared)
 headers_module (shared)
 http2_module (shared)
 include_module (shared)
 info_module (shared)
 log_config_module (shared)
 logio_module (shared)
 macro_module (shared)
 mime_magic_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 remoteip_module (shared)
 reqtimeout_module (shared)
 request_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 slotmem_plain_module (shared)
 slotmem_shm_module (shared)
 socache_dbm_module (shared)
 socache_memcache_module (shared)
 socache_shmcb_module (shared)
 status_module (shared)
 substitute_module (shared)
 suexec_module (shared)
 unixd_module (shared)
 userdir_module (shared)
 version_module (shared)
 vhost_alias_module (shared)
 watchdog_module (shared)
 dav_module (shared)
 dav_fs_module (shared)
 dav_lock_module (shared)
 lua_module (shared)
 mpm_prefork_module (shared)
 proxy_module (shared)
 lbmethod_bybusyness_module (shared)
 lbmethod_byrequests_module (shared)
 lbmethod_bytraffic_module (shared)
 lbmethod_heartbeat_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_express_module (shared)
 proxy_fcgi_module (shared)
 proxy_fdpass_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_hcheck_module (shared)
 proxy_scgi_module (shared)
 proxy_uwsgi_module (shared)
 proxy_wstunnel_module (shared)
 cgi_module (shared)
 wsgi_module (shared)

打开 Apache 的调试模式,我也进了日志:

AH02900: declining URL ws://127.0.0.1:5000/websockets/  (not WebSocket, Upgrade: header is missing)

Google chrome 的控制台向我显示了以下对 websocket 的请求:

Daphne 正在监听 0.0.0.0:5000 并且控制台显示没有对 websocket 的任何调用 -> 问题出在 Apache 服务器的配置中。

【问题讨论】:

    标签: django amazon-web-services websocket amazon-elastic-beanstalk daphne


    【解决方案1】:

    解决了将我的 03_proxy.config 更改为:

    files:
      "/etc/httpd/conf.d/proxy.conf":
        mode: "000644"
        owner: root
        group: root
        content: |
          ProxyPass /websockets/ ws://127.0.0.1:5000/websockets/ upgrade=NONE
          ProxyPassReverse /websockets/ ws://127.0.0.1:5000/websockets/
          #Uncomment below for Option 3
          #ProxyPass / http://127.0.0.1:5000/
          #ProxyPassReverse / http://127.0.0.1:5000/
    

    还需要把 asgi.py 改成:

    import os
    import django
    from channels.routing import get_default_application
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dashboard.settings')
    
    django.setup()
    
    application = get_default_application()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-02
      • 2015-06-09
      • 2017-12-13
      • 2015-08-10
      • 2020-10-29
      • 2018-03-27
      • 2019-11-25
      • 2016-04-08
      相关资源
      最近更新 更多