如何修复“ WebSocket握手期间错误:意外的响应代码:400”错误

时间:2019-08-02 10:58:27

标签: python docker nginx flask socket.io

我已经在docker中部署了flask socketio应用程序。但是,当我将其移到ssl安全域(从http到https)时,我的套接字连接开始抛出该错误“ WebSocket握手期间出错:意外的响应代码:400”,我不知道如何解决它。

我的docker-compose yml

version: '3'
services:

  rirlit-chat-server:
    build:
      context: ./docker/rirlit-chat-server
      dockerfile: Dockerfile
      args:
        git_ssh_key_path: /root/.ssh/id_rsa
        git_host: gitlab.com
        git_branch: dev
    container_name: rirlit-chat-server
    restart: always
    tty: true
    expose:
      - "7000"
    networks:
      - rirlit-network

  rirlit-nginx:
    build:
      context: ./docker/rirlit-nginx
      dockerfile: Dockerfile
      args:
        git_ssh_key_path: /root/.ssh/id_rsa
        git_host: gitlab.com
        git_branch: dev
    container_name: rirlit-nginx
    image: nginx:stable
    restart: always
    tty: true
    ports:
      - "80:80"
      - "443:443"
   networks:
      - rirlit-network

# Networks and volumes definition
networks:
  lirlit-network:
    driver: bridge

这是我的Nginx默认值

server {

    root /var/www/html;

    index index.html index.php index.htm index.nginx-debian.html;

    server_name lirlit.com;

    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

                root /var/www/lirlit-front-prod;
    }


  location /lirlit/api {
          rewrite ^/api/?(.*)$ /$1 break;
          include proxy_params;
          proxy_pass http://lirlit-chat-server:7000;
  }

  location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://lirlit-chat-server:7000/socket.io;
  }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/lirlit.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/lirlit.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = lirlit.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name lirlit.com;
    return 404; # managed by Certbot


}

1 个答案:

答案 0 :(得分:1)

您无需在proxy_pass上传递路径,该位置的正确代理配置为:

location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://lirlit-chat-server:7000;
  }