我有:
Apache 2.4 在我服务器的 80 端口上, mod_proxy 和 mod_proxy_wstunnel 启用
Node.js +
socket.io
在同一个服务器的3001端口
访问 example.com
(使用端口 80)重定向到 2。感谢 this method使用以下 Apache 配置:
<VirtualHost *:80>
ServerName example.com
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
ProxyPass / ws://localhost:3001/
ProxyPassReverse / ws://localhost:3001/
</VirtualHost>
它适用于一切,除了 websocket 部分:ws://...
不像代理应该那样传输。
当我访问 example.com
上的页面时,我有:
Impossible to connect ws://example.com/socket.io/?EIO=3&transport=websocket&sid=n30rqg9AEqZIk5c9AABN.
问题:如何让 Apache 也代理 WebSockets?
最佳答案
感谢this topic,我终于成功了.待办事项:
1) 安装 Apache 2.4(不适用于 2.2),然后:
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel
2) 让 nodejs
在端口 3001 上运行
3) 在 Apache 配置中执行此操作
<VirtualHost *:80>
ServerName example.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>
注意:如果您在使用 websockets 的同一服务器上有多个服务,您可能需要 do this将它们分开。
关于node.js - WebSockets 和 Apache 代理 : how to configure mod_proxy_wstunnel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27526281/