tabuna / web-socket

Laravel library for asynchronously serving WebSockets.
MIT License
220 stars 30 forks source link

Keepalive requests #14

Closed velegip closed 6 years ago

velegip commented 6 years ago

Hi!

The clients disconnects after some seconds.

Can you set keepalive requests in ws server?

tabuna commented 6 years ago

Please specify what you are using:

This should be a web server problem, not an application

velegip commented 6 years ago

Ratchet on port 5900 nginx proxy this to https://host/wsapp

tabuna commented 6 years ago

Simple nginx config (exemple.com)

# Default server configuration
#

server {

    server_name www.exemple.com;
    return 301 $scheme://exemple.com$request_uri;
}

map $http_upgrade $connection_upgrade {

    default upgrade;
    '' close;
}

upstream websocket {

    server exemple.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/exemple.com/public/;

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

    server_name exemple.com;

    location / {

        try_files $uri $uri/ /index.php?$query_string;
        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|
        mkv|mov|mp4|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {

            expires max;
            try_files $uri @fallback;
            limit_req zone=one burst=30;
        }

        location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css)$ {

            expires max;
        }

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {

        limit_req zone=one burst=3;
        include snippets/fastcgi-php.conf;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        # With php7.0-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        #With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

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', 'exemple.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'),
];
velegip commented 6 years ago

My configuration is :

upstream websocket { server 127.0.0.1:5300; } map $http_upgrade $connection_upgrade { default upgrade; '' close; }

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

*** host

    server_name ***;
    root /var/www/laravel/public;
    index index.php index.html index.htm default.html;

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

    location ~ ^/wsapp(/?)(.*)$ {
        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;
    }

    # pass the PHP scripts to FastCGI server
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    # optimize static file serving
    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
            access_log off;
            log_not_found off;
            expires 30d;
    }

    # deny access to .htaccess files, should an Apache document root conflict with nginx
    location ~ /\.ht {
            deny all;
    }

#...** the route of my ssl keys
ssl_certificate **;
ssl_certificate_**;
include **;
ssl_dhparam **;

if ($scheme != "https") {
    return 301 https://$host$request_uri;
} # managed by Certbot

}

tabuna commented 6 years ago

Try changing

  location ~ ^/wsapp(/?)(.*)$ {
        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;
    }

On how u done in my example


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;
    }
}
bellib commented 6 years ago

appach server ?

velegip commented 6 years ago

i don't wnat to run websocket as standalone 443 port.

i have a site on SSL same domain. So my nginx configuration proxys the websocket to path. https://example.com

Then then websocket on https://example.com/wsapp/{routes}

i figured out how to make this work, so i'm happy now. Same domain, same nginx config, same port.

sorry for my bad english.