1

I have been trying to deploy my chat app on my home Ubuntu server. It works locally when i connect to it using the internal ip or local server hostname.

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 proxy:

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. 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?

2
  • Perhaps the DNS records for your external address hasn't propagated to your to your home network? I can hit the external address fine 👍🏻 Commented Jul 2, 2019 at 2:07
  • It has been a week since I added the DNS, it has propagated. It loads the external on another network but I am not able to use the external domain when on the same network. Works on my work pc. But the issue is no longer at hand with the fix below, but that has now caused a diff issue that makes it not work any longer again... Commented Jul 2, 2019 at 3:52

2 Answers 2

1

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";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment, I copied/pasted your config, renamed the servername to my domain and I changed my project port to match what you have listed. Opened the ports as well. It now gives me a different error: polling-xhr.js:261 GET tfmserver.dynu.net/socket.io/… 502 (Bad Gateway) Both locally and externally on another network. But the other error is fixed I think! :D what can I do about this new error? It seems to me that it is not able to find the correct hostname to connect through? Thanks for the help
1

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. 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:

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. The other additions were recommended by Jairo Malanay and I added them in, which fixed the initial connection refusal problem I had.

I was having an issue with the CSS not loading in either and when adding the /socket.io/ this problem was resolved as well.

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.