gpt4 book ai didi

angularjs - DigitalOcean 上的 NodeJS 服务器使用 socket.io 返回连接被拒绝

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:13 25 4
gpt4 key购买 nike

我试图将 socket.io 引入到我用 Laravel 和 AngularJS 开发的应用程序中。
该应用程序在我的计算机上运行良好,但是当我尝试使其在服务器上运行时,我收到错误“GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=LQxpNjO” net::ERR_CONNECTION_REFUSED'。

服务器运行的是 Ubuntu 16.04,我使用 nginx 作为 Web 服务器。

这是创建 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');
});

});

这是应该连接到 Nodejs 服务器的 AngularJS 工厂。这里我使用的是 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):

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

我像这样更新了 nginx 配置文件:

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 工厂是这样的:

angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://localhost:8080/socket.io');

socket = socketFactory({
ioSocket: ioSocket
});

return socket;
});

现在我收到以下错误:
- 获取http://example.com/bower_components/socket.io-client/socket.io.js


更新2

这些是我的 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;
}
}

和 AngularJS 工厂:

angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://localhost');

socket = socketFactory({
ioSocket: ioSocket
});

return socket;
});




工作解决方案

感谢约翰,我终于成功地使应用程序运行起来。

这是 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;
}
}

这是 AngularJS 工厂:

angular.module('myServices').factory('socket', function (socketFactory) {
var ioSocket = io.connect('http://example.com');

socket = socketFactory({
ioSocket: ioSocket
});

return socket;
});

这是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 在您的域名后使用/socket.io。然后你必须指定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;
}

并且不要忘记 Node js 应用程序的上游

upstream app_yourdomain {
server 127.0.0.1:3000;
keepalive 8;
}

关于angularjs - DigitalOcean 上的 NodeJS 服务器使用 socket.io 返回连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com