Apache2 WebSockets reverse proxy on same URL

前端 未结 2 1391
孤城傲影
孤城傲影 2020-12-30 08:12

How to configure Apache2 to proxy WebSocket connection (BrowserSync for example), if it\'s made on the same URL, with only difference being header \"Upgrade: websocket\" and

相关标签:
2条回答
  • 2020-12-30 08:16

    Thanks a lot to metalim! I publish the full configuration here for reference:

    <VirtualHost *>
        ServerName example.org
        ServerAlias *.example.org
        ServerAdmin sysadmin@example.org
        ProxyRequests Off
        ProxyPreserveHost On
    
        RewriteEngine On
        RewriteCond %{HTTP:Connection} Upgrade [NC]
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteRule /(.*) ws://192.168.1.1/$1  [P,L]
    
        ProxyPass / http://192.168.1.1/ retry=1
        ProxyPassReverse / http://192.168.1.1/
    
        ErrorLog /var/log/apache2/example.org.error.log
        CustomLog /var/log/apache2/example.org.access.log combined
    </VirtualHost>
    
    0 讨论(0)
  • 2020-12-30 08:37

    Answering myself.

    Using RewriteEngine, hint given by this post, and WebSocket handshake specification:

    RewriteEngine On
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://127.0.0.1:3000/$1 [P,L]
    
    ProxyRequests off
    <Location />
        ProxyPass http://127.0.0.1:3000/
        ProxyPassReverse /
    </Location>
    
    0 讨论(0)
提交回复
热议问题