tabuna / web-socket

Laravel library for asynchronously serving WebSockets.
MIT License
219 stars 29 forks source link

connect() failed (111: Connection refused) #18

Closed diegocpires closed 6 years ago

diegocpires commented 6 years ago

Hi

Im trying to use this lib, but my client not connect on server. I'm using docker. Im my docker config, i foward 883 to 443.

My config of Ngnix

upstream websocket {
    server 127.0.0.1:5300;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    index index.php index.html;
    root /var/www/pdf/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ ^/ws(/?)(.*)$ {
        proxy_pass http://websocket/$2$is_args$args;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
    }
}

server {
    listen 443;
    server_name  localhost;
    location / {

        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
    }
}

routes/socket.php

<?php

use App\Http\Sockets\WebSocketServer;
/*
 *  Routes for WebSocket
 *
 * Add route (Symfony Routing Component)
 * $socket->route('/myclass', new MyClass, ['*']);
 */
$socket->route('/pdf', new WebSocketServer(), ['*']);

config/socket.php

<?php

return [

    /*
     * $httpHost HTTP hostname clients intend to connect to.
     * MUST match JS `new WebSocket('ws://$httpHost')`.
     */
    'httpHost'    => env('SOCKET_HTTP_HOST', '127.0.0.1'),

    /*
     * Port to listen on. If 80, assuming production,
     * Flash on 843 otherwise expecting Flash to be proxied through 8843
     */
    'port'        => env('SOCKET_PORT', '5300'),

    /*
     * Public port for Nginx
     */
    'public_port' => env('SOCKET_PUBLIC_PORT', '443'),

    /*
     * IP address to bind to. Default is localhost/proxy only.
     * `0.0.0.0` for any machine.
     */
    'address'     => env('SOCKET_ADDRESS', '0.0.0.0'),
];

JS

var socket = new WebSocket("ws://localhost:8843/pdf");

socket.onopen = function() {
  alert("The connection is established.");
};

Error

web_1  | 2018/02/28 12:22:36 [error] 7#7: *37 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /ws/pdf HTTP/1.1", upstream: "http://127.0.0.1:5300/pdf", host: "localhost:8086"
web_1  | 172.18.0.1 - - [28/Feb/2018:12:22:36 +0000] "GET /ws/pdf HTTP/1.1" 502 173 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"

Anyone can help me?

tabuna commented 6 years ago

Hi, why do you use port 8843 in ws://localhost:8843/pdf? This port is not even defined.

Should be 443 or 5300 (This is what is written in your configs).

And the error says about 8086. This is clearly a configuration error. Try to build the configuration differently. To make sure that the code actually works, you can access the socket directly, bypassing the nginx.

diegocpires commented 6 years ago

Thanks for the answer, @tabuna.

I use 8843 in connection, because my code is in a docker. And, the port 443 (on docker) is 8843 in my host.

I will try more configurations, but I have already tested almost all.

diegocpires commented 6 years ago

@tabuna

I changed configs, but still not working.

See:

Nginx

upstream websocket {
    server 127.0.0.1:8888;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    index index.php index.html;
    root /var/www/pdf/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

}

server {
    listen 8880;
    server_name  127.0.0.1;
    location / {

        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
    }
}

socket.php

<?php

return [

    /*
     * $httpHost HTTP hostname clients intend to connect to.
     * MUST match JS `new WebSocket('ws://$httpHost')`.
     */
    'httpHost'    => env('SOCKET_HTTP_HOST', '127.0.0.1'),

    /*
     * Port to listen on. If 80, assuming production,
     * Flash on 843 otherwise expecting Flash to be proxied through 8843
     */
    'port'        => env('SOCKET_PORT', '8888'),

    /*
     * Public port for Nginx
     */
    'public_port' => env('SOCKET_PUBLIC_PORT', '8880'),

    /*
     * IP address to bind to. Default is localhost/proxy only.
     * `0.0.0.0` for any machine.
     */
    'address'     => env('SOCKET_ADDRESS', '0.0.0.0'),
];

JS

var socket = new WebSocket("ws://127.0.0.1:8880/pdf");

socket.onopen = function() {
  alert("The connection is established.");
};

Error

web_1  | 172.18.0.1 - - [28/Feb/2018:17:06:09 +0000] "GET /pdf HTTP/1.1" 502 173 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"
web_1  | 2018/02/28 17:06:09 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: 127.0.0.1, request: "GET /pdf HTTP/1.1", upstream: "http://127.0.0.1:8888/pdf", host: "127.0.0.1:8880"

Look, 502 error. =(

Trying to connect direct via CURL

curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: 127.0.0.1:8888/pdf" -H "Origin: http://127.0.0.1" http://127.0.0.1:8888/pdf

Error via CURL

curl: (56) Recv failure: Connection reset by peer

Thanks in advanced by time spend.

tabuna commented 6 years ago

Do you reboot the artisan service? What happens if you connect to the port artisan? var socket = new WebSocket ("ws: //127.0.0.1: 8888 / pdf");

I can show my config completely if it helps you

diegocpires commented 6 years ago

Yes, i restarted artisan service.

Nothing happens when i try to connect directly. Browser throws an error saying its not possible to connect.

If you can, share with me. If is better, send to my email. diegopires at php dot net

Tks

tabuna commented 6 years ago

example of my configuration:

socket.php

<?php

return [

    /*
     * $httpHost HTTP hostname clients intend to connect to.
     * MUST match JS `new WebSocket('ws://$httpHost')
     */
    'httpHost'    => env('SOCKET_HTTP_HOST', 'mysite.com'),

    /*
     * Port to listen on. If 80, assuming production,
     * Flash on 843 otherwise expecting Flash to be proxied through 8843
     */
    'port'        =>  env('SOCKET_PORT', '5300'),

    /*
     * Public port for Nginx
     */
    'public_port' => env('SOCKET_PUBLIC_PORT', '443'),

    /*
     *IP address to bind to. Default is localhost/proxy only.
     *'0.0.0.0' for any machine.
     */
    'address'     => env('SOCKET_ADDRESS', '0.0.0.0'),
];

nginx.conf

# Default server configuration
#

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {

    worker_connections 1024;
    multi_accept on;
}

http {

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;
    server_tokens off;

    include /etc/nginx/mime.types;
    default_type text/javascript;

    access_log off;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_min_length 100;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    client_max_body_size 8M;

    server {
        server_name www.mysite.com;
        return 301 $scheme://mysite.com$request_uri;
    }

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server mysite.com:5300;
    }

    server {

        listen 443;
        location / {

            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_redirect off;
        }
    }

    server {

        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/treest/public/;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name mysite.com;

        location / {

            try_files $uri $uri/ /index.php?$query_string;
            location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css)$ {

                expires max;
            }
        }

        location ~ \.php$ {

            try_files $uri =404;
            include /etc/nginx/fastcgi.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

    }

}

supervisor

[program:laravel-socket]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/treest/artisan socket:serve
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/treest/storage/logs/socket.log
tabuna commented 6 years ago

I'm not sure, but maybe you need to add the ports to the whitelist IPTables?

diegocpires commented 6 years ago

@tabuna

Thanks a lot for all. In my case, the problem is in docker, ports and loopbacks on connection.

I will close this issue, ok?

Sorry for bothering!

tabuna commented 6 years ago

Did you succeed? Can you give an example, maybe someone from the participants in the future will have the same problem.

diegocpires commented 6 years ago

In my case, @tabuna, the only change i made it is to use internal ip of docker machine in the configs (10.0.1.155, for example) and JS instead of localhost or 127.0.0.1

My english is not so good, and i use google translate.