angularjs - El servidor NodeJS en DigitalOcean utilizando socket.io devuelve una conexión rechazada
node.js laravel (1)
Estaba intentando introducir socket.io en mi aplicación desarrollada en Laravel y en AngularJS.
La aplicación funciona bien en mi computadora, sin embargo, cuando intento que funcione en el servidor, aparece el error '' GET http: // localhost: 8080 / socket.io /? EIO = 3 & transporte = polling & t = LQxpNjO net :: ERR_CONNECTION_REFUSED '' .
El servidor ejecuta Ubuntu 16.04 y estoy usando nginx como servidor web.
Este es el archivo que crea el servidor nodejs:
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'');
});
});
Esta es la fábrica de AngularJS que debe conectarse al servidor nodejs. Aquí estoy usando angular-socket-io:
angular.module(''myServices'').factory(''socket'', function (socketFactory) {
var ioSocket = io.connect(''http://localhost:8080'');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
Este es el archivo de configuración de 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;
}
}
¿Cómo podría resolver este problema?
Actualización 1
Actualicé el archivo de configuración nginx de esta manera:
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;
}
y la fábrica de AngularJS así:
angular.module(''myServices'').factory(''socket'', function (socketFactory) {
var ioSocket = io.connect(''http://localhost:8080/socket.io'');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
Ahora recibo el siguiente error:
- OBTENGA http://example.com/bower_components/socket.io-client/socket.io.js
Actualización 2
Estos son mi archivo de configuración nginx:
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;
}
}
y la fábrica AngularJS:
angular.module(''myServices'').factory(''socket'', function (socketFactory) {
var ioSocket = io.connect(''http://localhost'');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
Solución de trabajo
Gracias a John finalmente logré que la aplicación funcionara.
Este es el archivo de configuración nginx:
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;
}
}
Esta es la fábrica de AngularJS:
angular.module(''myServices'').factory(''socket'', function (socketFactory) {
var ioSocket = io.connect(''http://example.com'');
socket = socketFactory({
ioSocket: ioSocket
});
return socket;
});
Este es el servidor nodejs:
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 usa /socket.io después de su nombre de dominio. Luego debe especificar la ubicación de socket.io:
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;
}
Y no te olvides del upstream para tu aplicación node js
upstream app_yourdomain {
server 127.0.0.1:3000;
keepalive 8;
}