Search code examples
javaandroidspring-bootspring-websocketstomp

Cannot connect to Tomcat web socket behind Apache


When I tried to connect to Spring Boot web socket from Android stomp client, it is not connecting and the Catalina log shows

Handshake failed due to invalid Upgrade header: null

Tomcat server is running behind Apache and the Apache server runs on https. I haven't added https in Tomcat .All the http requests are redirected to https this is how I tried to connect to the websocket

mStompClient = Stomp.over(Stomp.ConnectionProvider.JWS, "wss://chat.example.com/ws/chat/websocket", headers);

but it works when running in local machine

mStompClient = Stomp.over(Stomp.ConnectionProvider.JWS, "http://10.0.2.2:8080/chat/ws/chat/websocket", headers);

this is my stomp end point setup

registry.addEndpoint("/chat").setHandshakeHandler(new HandShakeHandler()).withSockJS();

I have enabled mod proxy wstunnel and in the virtual host config I have added

ProxyPass / http://localhost:8080/chat/
proxyPassReverse / http://localhost:8080/chat/
ProxyPass /wss/ ws://localhost:8080/chat/

How can I fix this?


Solution

  • I got the answer from this server fault lin. I have to add

    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule /api/(.*) ws://newapp.example.com:8080/api/$1 [P]
    

    and changed the last line to

    RewriteRule /chat/(.*) ws://localhost:8080/chat/chat/$1 [P]
    

    and now it is connected