Reverse WebSocket proxying using nginx
WebSocket is a protocol utilizing HTTP/1.1 as a connection mechanism via the Upgrade header on a request. Initially intended to provide the same functionality as SNI for TLS and SSL systems, this the Upgrade header now provides WebSocket support.
This is an expirimental configuration for using nginx as a reverse proxy for WebSocket's within Docker to connect to a node instance. To be able to use WebSocket support, nginx v1.4 is required.
nginx configuration
The following is hopefully minimal example of the configuration required for websockets. I've elected to use the upstream block out of convention, as the next step is usually going to be to cluster the system.
upstream app { server app_host; } server { listen 80; location / { proxy_pass http://app; proxy_http_version 1.1; # websockets only exist over HTTP/1.1 proxy_set_header Upgrade $http_upgrade; // Request upgrade to WebSocket protocol if client requested proxy_set_header Connection "upgrade"; //Specifiy the upgrade header is for the target only (TODO: Is this really needed?) } }
Application Service
Since I'm attempting to source the minimum configuration for nginx I've elected to use a simple echoing service built using (Express 4 && Socket.IO)/NodeJS. In theory this configuration should work with any application WebSocket system. TODO: Include fragment via gist
Client
To make life simple, I've created an application using jQuery and the Socket.IO client library. TODO: Include fragment via gist