Search code examples
javaspringapachetomcatwebsocket

Websocket returns 200 instead of 101 by Apache Server


I am trying to redirect traffic from Apache to Tomcat on CentOs server by using mod_proxy and mod_proxy_wstunnel modules. HTTP traffic is redirected without problems but I am not able to successfully redirect websocket traffic with any configuration I tried so far. It gives me 200 response code instead of 101.

I have read a lot of similar questions, but haven't find any solution yet. These one have similar problems. Question 1, Question 2

I'm using Apache Server 2.4.6 - > Apache Tomcat 7.0.92 - > Java Application with Spring and javax socket implementation.

Here is my modules:

enter image description here

Here is my httpd config:

<VirtualHost *:80>
    ServerName domain.com
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"$
    CustomLog /var/log/httpd/log_access.log combined
    ErrorLog /var/log/httpd/log_error.log

    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On
    <Proxy *>
       Require all granted
    </Proxy>

    RewriteEngine On

    #RewriteCond %{HTTP:Connection} Upgrade [NC]
    #RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
    RewriteRule /app-api/chat/user/(.*)   ws://127.0.0.1:8080/app-api/chat/user/$1   [P,L]

    ProxyPass /app-api http://127.0.0.1:8080/ retry=1 acquire=3000
    ProxyPassReverse /app-api http://127.0.0.1:8080/
    DocumentRoot /var/www/html

If i use connect to Apache Tomcat directly on local machine - all is good, it's returns 101,but not on remote.

This is remote CentOs machine. It gives 200.

Why i'm getting 200 response? What is wrong with this config?

I have tried a lot of implementaions, but still have 200 response from server instead of upgrade to 101.


Solution

  • Finnally, i have done it.

    In my case, there is no need to put RewriteConds and RewriteRules. All was need is to proxy request to actual backend (Apache Tomcat) by adding into httpd.conf

     ProxyPass /api/chat/user/  ws://127.0.0.1:8080/chat/user/
     ProxyPassReverse /api/chat/user/ ws://127.0.0.1:8080/chat/user/
    

    Also mod_proxy_wstunnel must be enabled.

    Got this config just from the official docs. Just in case: mod_proxy_wstunnel