Nginx – http connect tunnel to proxy in front of a websockets server

nginxPROXYsocket

I am having a hard time with this one. We have a websocket server listening on port 80 at myserver.com/wsDemo?ID=12. We need to test a client program by connecting it to this server through a proxy. I am trying nginx 1.2.7 as the proxy on port 8080, running on proxy-server. We want the client to connect to the proxy using http connect method, and then have the proxy create a tunnel to the myserver.com/wsDemo?ID=12 websocket app server. Here is my nginx config:

http {
include       /etc/nginx/mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

gzip  on;
server{
    listen  8080;
    listen  80;

    location /wsDemo?ID=12 {
            proxy_pass http://myserver.com:80/wsDemo?ID=12;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }
}

# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;

}

I would be happy to use any proxy to accomplish this if anyone has a different tool they would recommend (please add a config example is possible)

Best Answer

nginx currently does not natively support the WebSocket protocol. The first development version supporting it is 1.3.13 from Feb 20th (source: nginx.org).

Until it reaches a production-ready status, you might want to try the nginx_tcp_proxy_module from yaoweibin to proxy WebSocket connections as plain TCP.

Related Topic