Node.js - Good WebServer with proxy support and SSL WebSocket support?

I really like node.js, but it really complicates when you want to run multiple websocket servers and make them available through port 80.

I am currently running nginx, but proxying inbound web socket connections to different website servers depending on the URL is not possible because nginx does not support http 1.1.

I tried to implement a web server that has its own functions, but it is very difficult when it comes to passing the header, etc. Another thing is SSL support. This is not easy to maintain.

So, does anyone know a good solution to do what I mentioned?

Thanks for any help!

+4
source share
2 answers

I had good results using nodejitsu's node-http-proxy . As stated in their readme, they seem to support WebSockets.

Example for WebSockets (taken from their GitHub readme):

var http = require('http'), httpProxy = require('http-proxy'); // // Create an instance of node-http-proxy // var proxy = new httpProxy.HttpProxy(); var server = http.createServer(function (req, res) { // // Proxy normal HTTP requests // proxy.proxyRequest(req, res, { host: 'localhost', port: 8000 }) }); server.on('upgrade', function(req, socket, head) { // // Proxy websocket requests too // proxy.proxyWebSocketRequest(req, socket, head, { host: 'localhost', port: 8000 }); }); 

This usage should not be a problem as it is used for nodejitsu.com . To start a proxy application as a daemon, consider using forever .

+8
source

Newer versions of nginx will support reverse proxy for http / 1.1. You probably need version 1.1.7 or higher.

Try something like this in your configuration:

 location / { chunked_transfer_encoding off; proxy_http_version 1.1; proxy_pass http://localhost:9001; proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host:9001; #probaby need to change this proxy_set_header Connection "Upgrade"; proxy_set_header Upgrade websocket; } 

The nice thing is that you can complete SSL on nginx.

+1
source

Source: https://habr.com/ru/post/969351/


All Articles