WebSockets和Apache代理:如何configurationmod_proxy_wstunnel?
我有 :
- 
我的服务器的端口80上的 Apache(v2.4),用于www.domain1.com,启用了mod_proxy和mod_proxy_wstunnel
- 
同一台服务器的端口3001上的 node.js + socket.io。
 访问www.domain2.com (端口80)redirect到2.感谢这里描述的方法 。 我已经在Apacheconfiguration中设置了这个: 
 <VirtualHost *:80> ServerName www.domain2.com ProxyPass / http://localhost:3001/ ProxyPassReverse / http://localhost:3001/ ProxyPass / ws://localhost:3001/ ProxyPassReverse / ws://localhost:3001/ </VirtualHost> 
 它适用于一切,除了websocket部分: ws://...并不像代理服务器那样传输。 
 当我访问www.domain2.com上的网页时,我有: 
 Impossible to connect ws://www.domain2.com/socket.io/?EIO=3&transport=websocket&sid=n30rqg9AEqZIk5c9AABN. 
问题: 如何使Apache Proxy成为WebSockets?
我终于设法做到了,感谢这个话题 。
去做:
1)安装了Apache 2.4(不适用于2.2),并且:
 a2enmod proxy a2enmod proxy_wstunnel 
  2)让nodejs在端口3001上运行 
3)在Apacheconfiguration中执行此操作
 <VirtualHost *:80> ServerName www.domain2.com RewriteEngine On RewriteCond %{REQUEST_URI} ^/socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule /(.*) ws://localhost:3001/$1 [P,L] ProxyPass / http://localhost:3001/ ProxyPassReverse / http://localhost:3001/ </VirtualHost> 
您也可以通过HTTP标头进行筛选,而不是通过URL进行筛选。 这个configuration适用于任何使用websocket的web应用程序,如果它们不使用socket.io:
 <VirtualHost *:80> ServerName www.domain2.com RewriteEngine On RewriteCond %{HTTP:Upgrade} =websocket [NC] RewriteRule /(.*) ws://localhost:3001/$1 [P,L] RewriteCond %{HTTP:Upgrade} !=websocket [NC] RewriteRule /(.*) http://localhost:3001/$1 [P,L] ProxyPassReverse / http://localhost:3001/ </VirtualHost> 
可能会有用的。 只是所有的查询都通过ws发送给节点
 <VirtualHost *:80> ServerName www.domain2.com <Location "/"> ProxyPass "ws://localhost:3001/" </Location> </VirtualHost> 
我的设置:
Apache 2.4.10(运行Debian)NodeJS(版本4.1.1)在端口3000上运行的应用程序,它接受path / api / ws下的WebSocket
正如@Basj上面提到的,确保启用了a2enmod代理和ws_tunnel。
这是解决我的问题的Apache conf文件的屏幕抓图:
  
 
希望有所帮助。
 从Socket.IO 1.0(2014年5月)开始,所有连接都以HTTP轮询请求开始( 此处为更多信息)。 这意味着除了转发WebSocketstream量之外,您还需要转发任何transport=polling HTTP请求。 
下面的解决scheme应该正确地redirect所有套接字stream量,而不redirect任何其他stream量。
- 
启用以下Apache2 mods: sudo a2enmod proxy rewrite proxy_http proxy_wstunnel
- 
在* .conf文件中使用这些设置(例如 /etc/apache2/sites-available/mysite.com.conf)。 我已经包括了评论来解释每一件:<VirtualHost *:80> ServerName www.mydomain.com # Enable the rewrite engine # Requires: sudo a2enmod proxy rewrite proxy_http proxy_wstunnel # In the rules/conds, [NC] means case-insensitve, [P] means proxy RewriteEngine On # socket.io 1.0+ starts all connections with an HTTP polling request RewriteCond %{QUERY_STRING} transport=polling [NC] RewriteRule /(.*) http://localhost:3001/$1 [P] # When socket.io wants to initiate a WebSocket connection, it sends an # "upgrade: websocket" request that should be transferred to ws:// RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteRule /(.*) ws://localhost:3001/$1 [P] # OPTIONAL: Route all HTTP traffic at /node to port 3001 ProxyRequests Off ProxyPass /node http://localhost:3001 ProxyPassReverse /node http://localhost:3001 </VirtualHost>
- 
我已经包含了一个额外的路由 /nodestream量部分,我觉得很方便, 在这里看到更多的信息。
在这些答案的帮助下,我终于得到了在Ubuntu Mate和Apache2上使用Apache2站点configuration工作的Raspberry Pi上运行的Node-RED的反向代理:
 <VirtualHost *:80> ServerName nodered.domain.com RewriteEngine On RewriteCond %{HTTP:Upgrade} =websocket [NC] RewriteRule /(.*) ws://localhost:1880/$1 [P,L] RewriteCond %{HTTP:Upgrade} !=websocket [NC] RewriteRule /(.*) http://localhost:1880/$1 [P,L] </VirtualHost> 
我也必须启用这样的模块:
 sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_wstunnel 
对于“轮询”运输。
Apache方面:
 <VirtualHost *:80> ServerName mysite.com DocumentRoot /my/path ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass /my-connect-3001 http://127.0.0.1:3001/socket.io ProxyPassReverse /my-connect-3001 http://127.0.0.1:3001/socket.io </VirtualHost> 
客户端:
 var my_socket = new io.Manager(null, { host: 'mysite.com', path: '/my-connect-3001' transports: ['polling'], }).socket('/'); 
用户此链接为ws的perfact解决schemehttps://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html
你必须做下面的步骤..
得到/ etc / apache2 / mods-available
步骤1
使用下面的命令启用模式proxy_wstunnel.load
$ a2enmod proxy_wstunnel.load
第2步
转到/ etc / apache2 / sites-available
并在虚拟主机内的.conf文件中添加以下行
ProxyPass“/ ws2 /”“ws:// localhost:8080 /”
ProxyPass“/ wss2 /”“wss:// localhost:8080 /”
注意:8080表示你的tomcat运行端口,因为我们要连接“ws”,在tomcat和tomcat中放置的war文件为“ws”服务apache。 谢谢
我的configuration
ws://localhost/ws2/ALLCAD-Unifiedcommunication-1.0/chatserver?userid = 4 =已连接
去做:
- 
安装了Apache 2.4(不适用于2.2), a2enmod proxy和a2enmod proxy_wstunnel.load
- 
在Apacheconfiguration中执行此操作 
 只需在文件中添加两行,其中8080是您的Tomcat运行端口<VirtualHost *:80> ProxyPass "/ws2/" "ws://localhost:8080/" ProxyPass "/wss2/" "wss://localhost:8080/" </VirtualHost *:80>