3

I wanted to configure nginx to proxy websocket data from a websocket web client to a TCP server using nginx.

This comes from the fact that a web client can't connect directly to a TCP server. Describe what you’ve tried

I tried to use the yaoweibin/nginx_tcp_proxy_module. It can be found here: https://github.com/yaoweibin/nginx_tcp_proxy_module .

I have used this nginx conf:

   tcp {
       upstream cluster {
           # simple round-robin
           server localhost:8889;
           server localhost:8889;
           check interval=3000 rise=2 fall=5 timeout=50000;
       }

       upstream ws {
           server 127.0.0.1:85;
       }

       server {
           listen 8888 ;
           proxy_pass cluster;
       }
   }

   http {

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

       upstream ws_backend {
         server localhost:8010;
       }

       upstream tcp_backend {
         server localhost:8888;
       }

       server {
               listen 85;

               location / {
                   proxy_pass http://tcp_backend;
                   proxy_http_version 1.1;
                   proxy_set_header Upgrade $http_upgrade;
                   proxy_set_header Connection $connection_upgrade;
               }

               location /status {
                   tcp_check_status;
               }

       }
   }

The observed behaviour:

  • when the websocket client tries to connect, the tcp server receives a data:
DATA 127.0.0.1: GET / HTTP/1.1
Upgrade: websocket
Connection: upgrade
Host: tcp_backend
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: dXQD1uqFI498I58eRmDFAw==
Sec-WebSocket-Protocol: echo-protocol
  • but the websocket client after a timeout rises a connection error:
Connect Error: Error: socket hang up

Can you help me please if you have an alternative solution ( using lua/nginx ) or something based on nginx.

Thank you

Soufiane
  • 31
  • 2

0 Answers0