使用socket.io的DigitalOcean上的NodeJS服务器返回拒绝的连接 [英] NodeJS server on DigitalOcean using socket.io returns connection refused
问题描述
我试图将socket.io引入我在Laravel和AngularJS中开发的应用程序.
该应用程序可以在我的计算机上正常运行,但是当我尝试使其在服务器上运行时,出现错误' GET
I was trying to introduce socket.io to my application developed in Laravel and in AngularJS.
The application works fine in my computer, nevertheless when I try to make it work on the server I get the error 'GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=LQxpNjO net::ERR_CONNECTION_REFUSED'.
服务器正在运行Ubuntu 16.04,并且我将nginx用作Web服务器.
The server is running Ubuntu 16.04 and I am using nginx as a web server.
这是创建nodejs服务器的文件:
This is the file which creates the nodejs server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
var port = 8080;
http.listen(port, function() {
console.log('Listening on *:' + port);
});
io.on('connection', function (socket) {
console.info('Client connected');
redis.subscribe('counter.increase');
redis.on('message', function (channel, message) {
console.log('Received message ' + message + ' in channel ' + channel);
socket.emit(channel, message);
});
socket.on('disconnect', function() {
console.info('Client disconnected');
});
});
这是应该连接到nodejs服务器的AngularJS工厂.在这里,我正在使用angular-socket-io:
This is the AngularJS factory that should connect to the nodejs server. Here I am using angular-socket-io:
angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://localhost:8080');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
这是nginx的配置文件(/etc/nginx/sites-available/default):
This is the configuration file of nginx (/etc/nginx/sites-available/default):
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name XXX.XXX.XXX.XXX;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
我该如何解决这个问题?
更新1
How could I solve this problem?
Update 1
我这样更新了nginx配置文件:
I updated the nginx configuration file like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name XXX.XXX.XXX.XXX;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.io {
proxy_pass http://localhost:8080;
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 ~ /\.ht {
deny all;
}
}
upstream app_yourdomain {
server 127.0.0.1:3000;
keepalive 8;
}
和AngularJS工厂是这样的:
and the AngularJS factory like this:
angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://localhost:8080/socket.io');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
现在我得到以下错误:
-GET http://example.com/bower_components/socket.io-client /socket.io.js
更新2
Now i get the following error:
- GET http://example.com/bower_components/socket.io-client/socket.io.js
Update 2
这些是我的Nginx配置文件:
These are my nginx configuration file:
upstream app_example.com {
server 127.0.0.1:3000;
keepalive 8;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name XXX.XXX.XXX.XXX;
location / {
# First attempt to serve request as file, then as
# directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ ^/(socket\.io) {
proxy_pass http://localhost:8080;
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 ~ /\.ht {
deny all;
}
}
和AngularJS工厂:
and the AngularJS factory:
angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://localhost');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
工作解决方案
Working solution
多亏了约翰一世,我终于设法使该应用程序正常工作了.
Thanks to John I finally managed to make the application work.
这是nginx配置文件:
This is nginx configuration file:
upstream app_example.com {
server 127.0.0.1:3000;
keepalive 8;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name XXX.XXX.XXX.XXX;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ ^/(socket\.io) {
proxy_pass http://localhost:3000;
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 ~ /\.ht {
deny all;
}
}
这是AngularJS工厂:
This is the AngularJS factory:
angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://example.com');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
这是nodejs服务器:
This is the nodejs server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
var port = 3000;
http.listen(port, function() {
console.log('Listening on *:' + port);
});
io.on('connection', function (socket) {
console.info('Client connected');
redis.subscribe('counter.increase');
redis.on('message', function (channel, message) {
console.log('Received message ' + message + ' in channel ' + channel);
socket.emit(channel, message);
});
socket.on('disconnect', function() {
console.info('Client disconnected');
});
});
推荐答案
Socket.io在您的域名后使用/socket.io.然后,您必须指定socket.io位置:
Socket.io use /socket.io after your domaine name. Then you have to specify the socket.io location :
location ~* \.io {
proxy_pass http://localhost:8080;
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;
}
不要忘记您的节点js应用程序的上游
And don't forget the upstream for your node js app
upstream app_yourdomain {
server 127.0.0.1:3000;
keepalive 8;
}
这篇关于使用socket.io的DigitalOcean上的NodeJS服务器返回拒绝的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!