Search code examples
nginxwebsocketdigital-oceanpm2

WebSocket ws nginx reverse proxy 502 bad gateway


I know many different subject on internet interesting with this and much of them starting like mine but truly hard subject I have a nodejs application which has working 2 different 1. for application on port 3000 and 2. for api. on port 3001 Also i have using WebSocket on port 3001 for some admin operations, anyway i have no any problem in local environment. I deployed project to digitalocean its a grate working environment and my nginx settings like below

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name 188.166.91.39;
    return 302 https://$188.166.91.39$request_uri;
}

server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/self-signed.conf;
        include snippets/ssl-params.conf;

        server_name 188.166.91.39;

        gzip              on;
        gzip_comp_level   2;
        gzip_min_length   1024;
        gzip_vary         on;
        gzip_proxied      expired no-cache no-store private auth;
        gzip_types        application/x-javascript application/javascript application/xml application/json text/xml text/css text$

        client_body_timeout 12;
        client_header_timeout 12;
        reset_timedout_connection on;
        send_timeout 10;
        server_tokens off;
        client_max_body_size 50m;

        expires 1y;
        access_log off;
        log_not_found off;
        root /var/www/prtzn/public/content;

        location ~ "^/images/(?<entity>\w+)/(?<id>\w+)/(?<width>[1-9][0-9][0-9]{1}|[1][0-9][0-9][0-9]{1})/(?<file>.+)$" {
                # /images/products/id/100/file.jpg >>> Proxy to internal image resizing server
                proxy_pass http://127.0.0.1:8888/resize/$entity/$id/$width/$file;
                proxy_cache images;
                proxy_cache_valid 200 30d;
        }

        location /assets/ {
                alias /var/www/prtzn/theme/assets/;
        }

        location /sw.js {
                root /var/www/prtzn/theme/assets/;
        }

        location ~ ^/(api|ajax|ws|images)/ {
                # Proxy to NodeJS
                expires off;
                proxy_pass       http://127.0.0.1:3001;
                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 $scheme;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        location / {
                try_files $uri @proxy;
        }

        location @proxy {
                # Proxy to NodeJS
                expires off;
                proxy_pass       http://127.0.0.1:3000;
                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 $scheme;
        }
}  

i have followed digitalocean instraction they are realy good i have installed application but there is a 502 Bad Gateway error on my https front ends. It has trying to connect and want to handshake but couldn't make that. Giving error like that wsUrl..: wss://188.166.91.39

app-0c847e67113cfb2f2a2f.js:1 WebSocket connection to 'wss://188.166.91.39/ws/dashboard?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZXMiOlsiYWRtaW4iXSwianRpIjoiNWM2YzUyNDNhY2ZmMzgyZGFhZDZiZGNhIiwiZW1haWwiOiJoYW1pdC55aWxkaXJpbS41ODM0QGdtYWlsLmNvbSIsImlhdCI6MTU1MTA0NzI5MiwiZXhwIjoxNTUxMzA2NDkyfQ.AQ96n9cT0Ntab3GqYT-bFbhRlAyZj3tL53K9QjzEonM' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

Maybe this will become very simple and understandable for you but i am not very experinced with this type of works and i couldn't understand is this sourcing from faint token or nginx configuration or is there another miss configuration


Solution

  • my application running with pm2 then i don't look to logs with pm2 commands after some researching i noticed that my web api does not working perfectly which could not seen by the command

    npm start process.json

    becouse my 2 application appearing working and online but one of them not using any memory so after noticing that i have tryed to

    to: npm start

    and i have seen other errors on there. i fixed mines. The real error is backend service is not running or not responding