Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
Apache websocket works with ProxyPass but not RewriteRule

I'm trying to enable websockets on an AWS ElasticBeanstalk Tomcat 8.5 web app. I have a custom apache config (abbreviated):

RewriteEngine On
RewriteOptions Inherit

ProxyRequests Off
ProxyPreserveHost on

# send websocket requests to tomcat with the websocket protocol
RewriteCond ${HTTP:Connection}  "Upgrade" [NC]
RewriteCond ${HTTP:Upgrade}     "websocket" [NC]
RewriteRule /(.*) "ws://localhost:8080/$1" [P,L]

# send all other requests to tomcat
ProxyPass           /   http://localhost:8080/  retry=0
ProxyPassReverse    /   http://localhost:8080/

In the web app, when I try to connect the client websocket to the server endpoint, I get this error:

websocket.js:372 WebSocket connection to 'wss://example.com/ws/sales' failed: Error during WebSocket handshake: Unexpected response code: 404

I verified in Chrome's developer tools that the Connection and Upgrade headers sent in the request to the above endpoint are correct. (Obviously I'm really connecting to my website, not example.com)

When I change the apache config to use ProxyPass instead of RewriteRule, it works perfectly! I don't want to do it this way because I don't want to proxy to ws simply based on the URI. I want to check the headers like you're supposed to!

ProxyRequests Off
ProxyPreserveHost on

# send websocket requests to tomcat with the websocket protocol
ProxyPass           /ws/    ws://localhost:8080/ws/ retry=0
ProxyPassReverse    /ws/    ws://localhost:8080/ws/

# send all other requests to tomcat
ProxyPass           /   http://localhost:8080/  retry=0
ProxyPassReverse    /   http://localhost:8080/
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I stumbled upon the solution to this simply by trial and error, which is to remove the L (last) flag from the RewriteRule. What I don't understand still is why that fixes it. I figured it would be good to post this answer anyway.

RewriteEngine On
RewriteOptions Inherit

ProxyRequests Off
ProxyPreserveHost on

# send websocket requests to tomcat with the websocket protocol
RewriteCond %{HTTP:Upgrade}     "websocket" [NC]
RewriteCond %{HTTP:Connection}  "Upgrade" [NC]
RewriteRule .* "ws://localhost:8080%{REQUEST_URI}" [P]

# send all other requests to tomcat
ProxyPass           /   http://localhost:8080/  retry=0
ProxyPassReverse    /   http://localhost:8080/
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda