I am using ws (non secured WebSocket) in a Spring Boot application. Once the client tries to connect to the server the following error appears:
Handshake failed due to invalid Upgrade header: null
The spring boot app runs on an Ubuntu server behind an Apache proxy. Apache is configured as follows:
<VirtualHost *:80>
DocumentRoot /var/www/myapp/public/
ServerAdmin webmaster@localhost
ServerName app.myapp.biz
RewriteEngine On
RewriteCond %{HTTP:Upgrade}^websocket$ [NC,OR]
RewriteCond %{HTTP:Connection}^upgrade$ [NC]
RewriteRule .* "ws:/127.0.0.1:8096/$1" [P,QSA,L]
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://127.0.0.1:8096/
ProxyPassReverse / http://127.0.0.1:8096/
RequestHeader set X-Forwarded-Proto http
RequestHeader set X-Forwarded-Port 80
SetEnv mongo_username aUser
SetEnv mongo_password aPassword
</VirtualHost>
The application.properties
for the Spring app are as follows:
debug=false
server.port=8096
server.address=127.0.0.1
server.forward-headers-strategy=native
server.tomcat.use-relative-redirects=true
server.tomcat.remoteip.protocol-header=x-forwarded-proto
server.tomcat.remoteip.remote-ip-header=x-forwarded-for
server.tomcat.remoteip.port-header=x-forwarded-port
When running the app on my developer machine everything works just fine. Once deployed on the server the aforementioned error occurs. I’ve googled a lot but couldn’t find something specific for this problem. I hope someone can help.
Info: The frontend app trying to connect is an Angular app using ng2-stompjs
.
I finally managed to find the error. The order of statements in the apache.conf
does matter! The working conf file looks like so:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName app.myapp.biz
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://127.0.0.1:8096/
ProxyPassReverse / http://127.0.0.1:8096/
RewriteEngine On
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://127.0.0.1:8096/$1" [P,L]
SetEnv mongo_username aUser
SetEnv mongo_password aPassword
</VirtualHost>