简体   繁体   English

具有Nginx反向代理的Ubuntu Server上的Nodejs / Socket.io-“失败:建立连接时出错:net :: ERR_CONNECTION_TIMED_OUT”

[英]Nodejs/Socket.io on Ubuntu Server with Nginx Reverse Proxy - “failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT”

I have been trying to deploy my chat app on my home Ubuntu server. 我一直在尝试在我的家用Ubuntu服务器上部署聊天应用程序。 It works locally when i connect to it using the internal ip or local server hostname. 当我使用内部IP或本地服务器主机名连接到它时,它在本地工作。

I am using an nginx reverse proxy to change over from http://localhost:3000 to my external domain so that I can access it via the internet externally: http://tfmserver.dynu.net/ 我正在使用Nginx反向代理从http:// localhost:3000切换到我的外部域,以便可以通过Internet外部访问它: http : //tfmserver.dynu.net/

Nginx proxy: Nginx代理:

server {
    listen 80;
    listen [::]:80;

    root /var/www/tfmserver.dynu.net/html;
    index index.html index.htm index.nginx-debian.html;

    server_name tfmserver.dynu.net;

    location / {
    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;
}

I seem to get errors similar to the following but it is sometimes different depending on what I attempt to do to fix it: 我似乎收到与以下内容类似的错误,但有时会有所不同,具体取决于我尝试修复该错误的方法:

WebSocket connection to 'ws://tfmserver.dynu.net/socket.io/?EIO=3&transport=websocket&sid=wQY_D0JOZm4VWGXgAAAA' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

or 要么

POST http://tfmserver.dynu.net/socket.io/?EIO=3&transport=polling&t=MklujE_&sid=fbdZir8lxOlMOZm6AAAA net::ERR_CONNECTION_TIMED_OUT

According to some posts people have made about this error they are saying that Chrome is trying it as SSL but it is not being served that way, however I have added SSL to the server and into the project but it does not resolve the issue. 根据有关此错误的人的一些帖子,他们说Chrome尝试将其作为SSL进行尝试,但这种方式并未得到解决,但是我已将SSL添加到服务器和项目中,但无法解决问题。 At the moment I have it removed, but would not mind adding it back in if possible once it is working. 目前,我已将其删除,但如果可以的话,不介意将其重新添加。

I've tried everything I can from all the possible other questions posted here, none are resolving the issue. 我已经尽力解决了这里发布的所有其他可能出现的问题,但都没有解决这个问题。

How can I get this to work externally? 我怎样才能使它在外部工作? What am I doing wrong? 我究竟做错了什么?

Here are the relevant parts of the project for the sockets. 这是插座项目的相关部分。 If you need anything else that could help, please let me know - Thanks in advance! 如果您还有其他需要帮助的地方,请告诉我-预先感谢!

server: 服务器:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

server.listen(process.env.PORT || 3000, 'localhost');

client: 客户:

var socket = io.connect();

UPDATE: - Note: I just connected to it from my work computer and it works?! 更新:-注意:我刚刚从工作计算机连接到它,并且它起作用了吗? But it does not work in my own network when trying to use the external address? 但是在尝试使用外部地址时,它在我自己的网络中不起作用吗? What's up with that? 那是怎么回事?

UPDATE: So I have finally resolved it (mostly)! 更新: 所以我终于解决了它(大部分)!

By mostly, I mean that I still am not able to use my domain to access this when I am at home on the same network as my server. 通常,我的意思是,当我在家中与服务器位于同一网络上时,仍然无法使用我的域来访问此域。 To view it I MUST use the internal IP to the server or the server's network hostname. 要查看它,我必须使用服务器的内部IP或服务器的网络主机名。 The domain works outside of the network, such as from work or elsewhere. 该域在网络外部工作,例如在工作场所或其他地方。 (I can live with that!) (我可以忍受这一点!)

The issue was with the Nginx Proxy, the final config that resolved the issue for me is as follows: 问题出在Nginx代理上,为我解决了该问题的最终配置如下:

server {
        listen 80;
        server_name tfmserver.dynu.net;
        client_max_body_size 800M;

        root /home/tfm/Projects/Chat;

        gzip on;
        gzip_comp_level 6;
        gzip_vary on;
        gzip_min_length  1000;
        gzip_proxied any;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_buffers 16 8k;

        location **/socket.io/** {
                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_set_header X-NginX-Proxy true;

                proxy_pass http://localhost:9000**/socket.io/**;
                proxy_redirect off;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

To fix the new error, the 502 bad gateway, I had to have " /socket.io/ " added to the location and to the hostname. 要解决新错误502错误的网关,我必须在位置和主机名中添加“ /socket.io/ ”。 The other additions were recommended by Jairo Malanay and I added them in, which fixed the initial connection refusal problem I had. Jairo Malanay推荐了其他添加项,我添加了它们,从而解决了我最初遇到的连接拒绝问题。

I was having an issue with the CSS not loading in either and when adding the /socket.io/ this problem was resolved as well. 我有一个CSS无法加载的问题,并且在添加/socket.io/时也解决了此问题。

My final serverside: 我的最终服务器端:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

server.listen(process.env.PORT || 9000, 'localhost');

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {

And my final clientside: 我的最终客户:

var socket = io.connect();

Thanks for the help again Jairo Malanay! 再次感谢您的帮助,Jairo Malanay!

I was able to make it work using my config. 我能够使用我的配置使其工作。 you need to consider the redirect and proxy 您需要考虑重定向和代理

server {
    listen 80;
    server_name 11.111.111.111;
    client_max_body_size 800M;

    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;

   location / {
        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_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:9000/;
        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }


  location ~* \.io {
        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_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:4001;
        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM