简体   繁体   中英

Proxypass socket io on Apache Server on Ubuntu

I'm trying to deploy node js socket.io project from official sample in my server (Ubuntu with Apache 2 Server)

This is my proxy setting in /etc/apache2/sites-available/000-default.conf

ProxyPass /ChatSocket http://localhost:3000
ProxyPass /socket.io http://localhost:3000/socket.io
ProxyPass /socket.io ws://localhost:3000/socket.io

But when I try to access my web server through the dns, I can't connect to the socket.

I got this error in my browser console

WebSocket connection to 'ws://example.com/socket.io/?EIO=3&transport=websocket&sid=HgU6py-fCl1GFrdsAAAK' failed: Error during WebSocket handshake: Unexpected response code: 400

Maybe because the url http://example.com/socket.io is conflicting with ws://example.com/socket.io

How can I use the websocket and the http with the same url? Because it seems that http://example.com/socket.io and ws://example.com/socket.io is generated by socket.io from this line

var server = require('http').createServer(app);
var io = require('socket.io')(server);

Are there any other workaround to fix this, or deploying my socket.io node js app in my apache server without proxypass?

Nevermind, I manage to get it working with RewriteEngine for the web socket ( ws:// )

This is my 000-default.conf in /etc/apache2/sites-available

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io          [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:3000/%1        [P,L]

ProxyPass /ChatSocket http://localhost:3000
ProxyPassReverse /ChatSocket http://localhost:3000
ProxyPass /socket.io http://localhost:3000/socket.io
ProxyPassReverse /socket.io http://localhost:3000/socket.io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM