我试图用apache设置proxyng,以便能够在我的nodejs应用程序中使用socket.io.但我在客户端收到此错误消息:
WebSocket connection to 'wss://example.com/tools/socket.io/?EIO=3&transport=websocket&sid=eOGwJSC14TTWhHRMAAAR' failed: Error during WebSocket handshake: Unexpected response code: 400
继承我的apache配置:
ServerName example.com ServerAlias example.com #SSLRequireSSL SSLProtocol all -SSLv2 -SSLv3 SSLCompression off SSLHonorCipherOrder on SSLEngine on SSLCertificateFile /etc/ssl/main.pem SSLCertificateKeyFile /etc/ssl/dec_ssl.key SSLCertificateChainFile /etc/ssl/sub.class1.server.ca.pem SSLCACertificateFile /etc/ssl/main.pem SSLProxyEngine On ProxyPreserveHost On ProxyRequests off ProxyPass http://localhost:8181/ ProxyPassReverse http://localhost:8181/
这是客户端代码:
socket = io.connect("https://example.com",{path:'/tools/socket.io'}); socket.on("connect", function () { console.log("socketio Connected to server!"); });
还有什么我需要添加到apache配置来摆脱这个错误?
编辑:我的apache版本:服务器版本:Apache/2.4.6(CentOS)
我已在Apache 2.4.16中成功使用了此功能,较旧的版本可能无法正常工作。
ServerName example.com ServerAlias example.com #SSLRequireSSL SSLProtocol all -SSLv2 -SSLv3 SSLCompression off SSLHonorCipherOrder on SSLEngine on SSLCertificateFile /etc/ssl/main.pem SSLCertificateKeyFile /etc/ssl/dec_ssl.key SSLCertificateChainFile /etc/ssl/sub.class1.server.ca.pem SSLCACertificateFile /etc/ssl/main.pem SSLProxyEngine On ProxyPreserveHost On ProxyRequests off RewriteEngine On RewriteCond %{REQUEST_URI} ^/tools/socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule "^/tools/socket.io" "ws://localhost:8181/socket.io/" [P,L] ProxyPass http://localhost:8181/ ProxyPassReverse http://localhost:8181/
ProxyPass will not handle websocket upgrades. So, what you want to do is redirect those requests to ws://
I think in general you should not be using Location to handle this, but this config should work for you.