Невозможно установить соединение через веб-сокет - React js + Node Express + Nginx - PullRequest
0 голосов
/ 22 февраля 2019

Помогите,
Не удалось получить следующую ошибку в производственном процессе

Соединение WebSocket с 'wss: //subdomain.example.com/socket.io/? EIO = 3 & transport = websocket':Ошибка во время рукопожатия WebSocket: неожиданный код ответа: 404

Вот моя конфигурация и код для Nginx - React Js & Node Express

Конфигурация Nginx

http {

    map $http_upgrade $connection_upgrade {
            default upgrade;
            '' close;
    }

    upstream node-js-myapp {
            server 127.0.0.1:5000;
    }

tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

server {
    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name subdomain.example.com;

    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.html;
        }
        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
          expires 1M;
          access_log off;
          add_header Cache-Control "public";
        }
        # Javascript and CSS files
        location ~* \.(?:css|js)$ {
            try_files $uri =404;
            expires 1y;
            access_log off;
            add_header Cache-Control "public";
        }
        # Any route containing a file extension (e.g. /devicesfile.js)
        location ~ ^.+\..+$ {
            try_files $uri =404;
        }
    location /app2/ {
                proxy_pass http://localhost:5000/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
        location ^~ /socket.io/ {
               try_files $uri @node-js-myapp;
        }
        location @node-js-myapp {
               proxy_set_header X-Real-IP  $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header Host $http_host;
               proxy_pass  http://node-js-myapp;
       }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/subdomain.example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/subdomain.example.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
}
}

React Js

import socketIOClient from 'socket.io-client';
const socket = socketIOClient(
  'wss://subdomain.example.com',
  { transports: ['websocket'] }
);
socket.on('socketToMe', function (data) {
  console.log(data);
  socket.emit('ABC', "abcdef");
});

Node Express

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

var server = require('http').Server(app);
var io = require('socket.io')(server);

io.on('connection', function(socket) {
 console.log("Con");

socket.on("ABC", (data) => {
 console.log(data);
 io.sockets.emit('socketToMe', "test test");
})
socket.on('disconnect', () => {
 console.log('user disconnected')
})
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...