Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Nginx服务器配置web套接字?_Javascript_Node.js_Nginx_Websocket - Fatal编程技术网

Javascript 如何使用Nginx服务器配置web套接字?

Javascript 如何使用Nginx服务器配置web套接字?,javascript,node.js,nginx,websocket,Javascript,Node.js,Nginx,Websocket,我正在使用Nginx服务器运行node.js和socket.io。 这是my/etc/nginx/conf.d/site.conf: server { listen 443; server_name localhost; client_max_body_size 20M; location /websocket/ { root ChatWithSocket; index index.html index.ht

我正在使用Nginx服务器运行node.js和socket.io。 这是my/etc/nginx/conf.d/site.conf:

server {
   listen 443;
   server_name localhost;

   client_max_body_size 20M;

   location /websocket/ {
               root ChatWithSocket;
               index index.html index.htm index.nginx-debian.html;
               proxy_pass http://localhost:4000;
               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;
 }


}
这是我的/ChatWithSocket/index.js:

var express = require('express');
var socket = require('socket.io');

// App setup
var app = express();
var server = app.listen(4000, function(){
    console.log('listening for requests on port 4000,');
});

// Static files
app.use(express.static('public'));

//Socket setup
var io = socket(server);

io.on('connection', function(socket){
    console.log('made socket connection', socket.id);

    //Show diconnect log when we refresh page
    socket.on('disconnect', function(){
        console.log('The socket disconnected');
    }); 

    // Handle chat event
    socket.on('chat', function(data){
        // console.log(data);
        io.sockets.emit('chat', data);
    });

    // Handle typing event
    socket.on('typing', function(data){
        socket.broadcast.emit('typing', data);
    });
});
这是我的/ChatWithSocket/public/chat.js:

// Make connection
var socket = io.connect('http://localhost:4000');

// Query DOM
var message = document.getElementById('message'),
      handle = document.getElementById('handle'),
      btn = document.getElementById('send'),
      output = document.getElementById('output'),
      feedback = document.getElementById('feedback'),
      chatWindow = document.getElementById('chat-window');
......................................................
T


我不能用地址跑步,因为我已经听过很多关于Nginx+WebSocket的在线指南,但我不知道我在哪里犯了错误。请帮助我。

使用此配置,然后重试。您不需要更新proxy_pass localhost:4000-->proxy_pass 40.115.176.82:4000。这是不正确的。问题在于客户

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

server {
   listen 443;
   server_name localhost;

   client_max_body_size 20M;

   # smart-shingi log_format data >>
   if ($http_token ~ "(\S{10})$") {
       set $h_token $1;
   }
   # << smart-shingi log_format data

   error_page  500 502 503 504  /50x.html;
   location = /50x.html {
       root  /usr/share/nginx/html;
   }

   location / {
       deny  all;
   }

   location ~ /\. {
       deny  all;
   }

   location = /favicon.ico {
       error_page     404 = @favicon;
       access_log     off;
       log_not_found  off;
   }

   location @favicon {
       empty_gif;
       access_log     off;
       log_not_found  off;
   }

   location /drive/ {
       root   /usr/share/nginx/webapp;
       index  login.html;
       allow  all;
   }


   location /websocket/ {
    proxy_pass http://localhost:4000;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;  # pass the host header - http://wiki$
    proxy_http_version 1.1;  # recommended with keepalive connections $

    ### WebSocket proxying - from http://nginx.org/en/docs/http/websocke$
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
}

   location ~ \.txt {
       root    /usr/share/nginx/androidApp;
   }
}

还要检查您正在使用的socketio版本。

在您的客户端中,您是否正在尝试连接到本地主机?而服务器托管在40.115.176.82?此外,在nginx中,您已配置为仅将前缀为/websocket/的url路由到socketio服务器。在客户端上尝试,
var socket=io('https://40.115.176.82“,{path:'/websocket/'})
以正确设置前缀和服务器。还可以读取您可以在socketio客户端上发布错误堆栈吗?
// Make connection
var socket = io('https://40.115.176.82', {path: ' /websocket/'})

// Query DOM
var message = document.getElementById('message'),
      handle = document.getElementById('handle'),
      btn = document.getElementById('send'),
      output = document.getElementById('output'),
      feedback = document.getElementById('feedback'),
      chatWindow = document.getElementById('chat-window');