我能够建立一个从clienJS到NodeJs的websocket连接。但是当请求通过apache httpd时,无法连接websocket。
使用Httpd2.4.7,我收到以下错误。请让我知道需要纠正的内容。
与'ws://172.27.38.86/socket.io/1/websocket/_uW8Sv7lgQfrZncTSzKu'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:502
谢谢&此致
Jawahar
答案 0 :(得分:2)
在2.4.5之后,apache在他们的主干上包含了一个名为proxy_wstunnel的模块,目前的ubuntu生产版本的apache(2.4.7)尚未提供。这有点痛苦,但大致遵循列出的步骤on this blog-post我设法安装模块并成功使用它。
dpkg -s apache2 //this gives you the version of apache in my case "2.4.7-1ubuntu4.1"
//then you checkout that version of apache
svn co http://svn.apache.org/viewvc/httpd/httpd/tags/2.4.7/
//you get into the directory just checked out
cd 2.4.7
//in there you checkout the Apache Portable Runtime Project and utils
svn co http://svn.apache.org/repos/asf/apr/apr/branches/1.4.x srclib/apr
svn co http://svn.apache.org/repos/asf/apr/apr-util/branches/1.3.x srclib/apr-util
//you compile with the corresponding modules flags
./buildconf
./configure --enable-proxy=shared --enable-proxy_wstunnel=shared
make
//You copy the modules (mod_proxy and mod_proxy_wstunnel) to your apache working copy
//It could be advisable to backup the old mods first
sudo cp modules/proxy/.libs/mod_proxy{_wstunnel,}.so /usr/lib/apache2/modules/
sudo chmod 644 /usr/lib/apache2/modules/mod_proxy{_wstunnel,}.so
sudo echo -e "# Depends: proxy\nLoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so" | sudo tee -a /etc/apache2/mods-available/proxy_wstunnel.load
//you then enable your module and restart your apache... so now the module is ready to use
sudo a2enmod proxy_wstunnel
sudo service apache2 restart
然后,研究了一下我发现this page我相应地配置了我的apache vhost文件
<VirtualHost *:80>
ServerAdmin yourmail@mail.com
ServerName yoursubdomain.yourdomain.info
Redirect permanent / https://yoursubdomain.yourdomain.info
</VirtualHost>
<VirtualHost *:443>
ServerAdmin yourmail@mail.com
ServerName yoursubdomain.yourdomain.info
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost On
//these next two lines are to enable the wstunnel
ProxyPass /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
ProxyPassReverse /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
//this line is to retrieve the socket.io.js to use
ProxyPass /socket.io/ http://localhost:9091/socket.io/
SSLEngine on
SSLCertificateFile /etc/ssl/certs/yourcert.crt
SSLCertificateKeyFile /etc/ssl/private/yourcert.key
SSLCertificateChainFile /etc/ssl/certs/your_bundle.crt
//your logs
CustomLog /var/log/apache2/yoursubdomain.yourdomain.info.log combined
ErrorLog /var/log/apache2/yoursubdomain.yourdomain.info.error.log
</VirtualHost>
和TaDaaa ......我有一个工作节点websocket通过一个apache代理在端口9091上响应,通过443标准ssl端口获取所有内容。
我想ubuntu很快会将这个模块包含在他们的制作版本中,但同时这是要走的路
答案 1 :(得分:1)
Apache SUCKS通过代理处理websockets。我建议要么删除Apache层,要么修改socket.io设置以使用XHR轮询。
答案 2 :(得分:0)
我必须在CentOs中设置ws隧道,默认情况下安装了apache 2.2.15。
我尝试用apache 2.2.15修补proxy_wstunnel模块。但没有帮助。最后我决定删除apache 2.2.15并按照http://httpd.apache.org/docs/2.4/install.html的官方文档安装apache 2.4
安装后(我已安装在默认位置/ usr / local / apache2 /),我执行了以下操作以获得隧道工作
取消注释/usr/local/apache2/conf/httpd.conf中的以下行
LoadModule proxy_module modules / mod_proxy.so
LoadModule proxy_wstunnel_module modules / mod_proxy_wstunnel.so
添加用于隧道化websocket请求的虚拟主机
<VirtualHost *:80>
ServerName subdomain.mydomain.com
ProxyPass / ws://localhost:8081/
ProxyPassReverse / ws://localhost:8081/
</VirtualHost>
从我的前端,websocket通过ws://subdomain.mydomain.com连接:80