我在 NGINX 中遇到了这个错误 - 失败:WebSocket 握手期间出错:意外响应代码:502我该如何解决这个问题?

问题描述

我在 AWS 中托管我的 django 聊天应用程序,在一个端口中使用 Nginx 和 wsgi 作为 gunicorn,在另一个端口中使用 Asgi 作为 daphne。现在 http 请求工作正常,但在发送 websocket 请求时出现此错误。 我在 /etc/systemd/system 中的 daphne.service 文件包含,

[Unit]
Description=myproject daphne Service
After=network.target
[Service]
Type=simple 
User=yourusername    # eg: User = ubuntu
WorkingDirectory=path_to_your_project # eg  /home/ubuntu/myproject  
ExecStart=daphne -p 9001 myproject.asgi:application 
#edit myproject
#If your daphne is not installed globally and is inside your #environment,enter the complete path to daphne
#ExecStart=/home/ubuntu/myproject/env/bin/daphne -p 9001     
#          myproject.asgi:application
[Install]
WantedBy=multi-user.target

这是我在 django.conf 中的 Nginx 配置文件

 #added this block
upstream channels-backend {
 server localhost:9001;
}
server {
 listen 80;
 server_name mydomain
location = /favicon.ico { access_log off; log_not_found off; }
 location /static/ {
 root /home/ubuntu/myproject;
 }

 location / {
 include proxy_params;
 proxy_pass http://unix:/run/myproject.sock;
 }
#path to proxy my WebSocket requests
location /ws/ {
 
 proxy_pass http://channels-backend;
 proxy_http_version 1.1;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection “upgrade”;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header X-Forwarded-Host $server_name;
 }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)