Search code examples
nginxwebsocketproxyreverse-dnshttp-status-code-502

failed to dial to (wss://<ip>/subf1/40234): 502 Bad Gateway > websocket: bad handshake


OK, this 502 error is from something different than the other related articles here. first i managed to setup a v2ray system using ubuntu 22. I have a domain and a cloudflare account to set DNS for my domain and a 'sub' subdomain connected to my server ip, all set as it should be. equipped with SSL cert & key using certbot on linux and works ok, website comes up with https just fine. dns proxy and websocket check are both enable in CF(CloudFlare). also im using reverse DNS like the famous tutorials saying, so i've edited the /etc/nginx/sites-available/sub.domain.com to become like this:

server {

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name sub.domain.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }


   location /subf1 {

     if ($http_upgrade != "websocket") {

        return 404;

     }


     location ~ /subf1/\d\d\d\d\d$ {

        if ($request_uri ~* "([^/]*$)" ) {

            set $port $1;

        }

        proxy_redirect off;

        proxy_pass http://127.0.0.1:$port/;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header 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;

    }

    return 404;

  }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #       include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
    #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #       fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}

 listen [::]:443 ssl ipv6only=on; # managed by Certbot
 listen 443 ssl; # managed by Certbot
 ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem; # mana>
 ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem; # ma>
 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {

  if ($host = sub.domain.com) {
    return 301 https://$host$request_uri;
  } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name sub.domain.com;
    return 404; # managed by Certbot


}

also, have the x-ui v2ray panel installed and everything is setup just right here is a sample vless connection which i used to create normally and worked fine till yesterday:

enter image description here

the 9988 port is defined from x-ui v2ray management panel on the server.

everything was fine until i removed the x-ui panel and installed another version. since then i cannot make the connections work, no matter what the v2ray client gives this series of errors:

app/proxyman/outbound: failed to process outbound traffic > proxy/vless/outbound: failed to find an available destination > common/retry: [transport/internet/websocket: failed to dial WebSocket > transport/internet/websocket: failed to dial to (wss://<ip>/subf1/40234): 502 Bad Gateway > websocket: bad handshake] > common/retry: all retry attempts failed

proxy/http: failed to read response from 149.xx.xx.xx:80 > io: read/write on closed pipe

some things i did to resolve this are:

  • restarting nginx
  • rebooting the server
  • re-installing the x-ui panel (various versions)
  • rebuilding the server
  • changing the subdomain
  • changing the sub domain + changing the server and gettin a different ip

and SSL is being set just fine with CF SSL selected at Full.

any kind of help and suggestion is so appreciated.


Solution

  • Ok, that was a problem from CDN ip used in the connection inbound.