简体   繁体   中英

How to configure Apache to proxy socket.io server

I have a webapp running on http://example.com:5000/

The website tries to establish a socket.io connection to that server to exchange some time critical data (multi player game).

Everything works fine that far...

Now I'm trying to get that whole thing production ready, so I want to make it available on a nicer URL: https://myapp.example.com/

To do so I configure my Apache server as follows:

ProxyPass / http://0.0.0.0:5000/
ProxyPassReverse / http://0.0.0.0:5000/

On the first glance everything seems to work - but socket.io can't switch from polling to websocket any more.

Javascript console shows this error:

socket.io.js:8 WebSocket connection to 'wss://myadpp.example.com/socket.io/?EIO=3&transport=websocket&sid=0bef58d5622f4d20b4c1d7f29c97a21d' failed: Error during WebSocket handshake: Unexpected response code: 200

It seems to be a very common setup - hiding a socket.io based webapp behind an apache proxy - but I can't find any working solutions online.

It seems that apache can't proxy ws:// connections, but I don't know what to do instead..

Found the solution here: WebSockets and Apache proxy : how to configure mod_proxy_wstunnel?

Try loading the mod_proxy_wstunnel module: https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html

Edit:

For you second question try this:

RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
RewriteCond %{QUERY_STRING} transport=websocket    [NC]
RewriteRule /(.*)           ws://0.0.0.0:5000/$1 [P,L]

RewriteCond %{QUERY_STRING} transport=polling    [NC]
RewriteRule /(.*)           http://0.0.0.0:5000/$1 [P,L]

ProxyPass / http://0.0.0.0:5000/
ProxyPassReverse / http://0.0.0.0:5000/

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