WebSocket Returns 200 Instead of 101 with Apache2: How to Properly Configure Proxy for WebSocket Handling?
I am trying to configure Apache2 to proxy WebSocket traffic for my application, but I’m facing an issue where the WebSocket connection returns a 200 status instead of the expected 101 status code. I have the following configuration in my default-ssl.conf file for SSL and WebSocket proxy:
ServerName <domain>
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /home/root/app
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
# WebSocket upgrade headers
SetEnvIf Request_URI "^/ws" upgrade_connection
RequestHeader set Connection "Upgrade" env=upgrade_connection
RequestHeader set Upgrade "websocket" env=upgrade_connection
# Proxy to WebSocket server
ProxyPreserveHost On
ProxyPass /wss/ wss://127.0.0.1:8080/
ProxyPassReverse /wss/ wss://127.0.0.1:8080/
# Proxy WebSocket traffic to Daphne (ASGI)
RewriteEngine On
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:8080%{REQUEST_URI} [P,QSA,L]
# Proxy settings
ProxyRequests on
ProxyPass / http://<ip>:8000/
ProxyPassReverse / http://<ip>:8000/
</VirtualHost>
Despite this configuration, the WebSocket connection is returning an HTTP 200 status instead of the expected WebSocket handshake (101). What could be causing this issue and how can I resolve it?