当前位置:  开发笔记 > 编程语言 > 正文

带有apache代理的Socket.io

如何解决《带有apache代理的Socket.io》经验,为你挑选了1个好方法。

我试图用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)



1> Gary..:

我已在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.

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有