I’m creating a website for farm management. I’m running a Asp Net Web API on CyberPanel. I’ve done it following this article: Publish & Run ASP.Net on CyberPanel / Linux – Blog of Viov
The problem is i’m using a technology called SignalR. The SignalR is basically a top level abstract layer to simplify web socket (it also include other functionalities but it don’t matter now). Everything was going as expected on the development environment but when I deploy the application and host the Asp Net on Cyberpanel the Web socket stops working.
The Cyberpanel error log prints this:
[2023-05-24 09:26:32.085918 [INFO] [18351] [187.74.137.142:46278#api.milksolve.com.br] Cannot find web socket backend. URI: [/fazendahub]
The client says that the Websocket didn’t send the handshake, so the Websocket definitely is not working. I checked the access log and there I confirmed that the Signalr protocol is broking on the web socket connection. Anyone has a solution?
Edit:
After some research I've figured out that this problem is related to the Virtual Host Reverse Proxy. CyberPanel uses the OpenLiteSpeed, and the only solutions that I've found on internet were to Ngix and Apache.
My Rewrite File:
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule /(.*) ws://127.0.0.1:5000/$1 [P]
Still not working with WebSocket
After some days of suffering I found my own solution. The first thing is to set up a Reverse Proxy as explained on this article: Publish & Run ASP.Net on CyberPanel / Linux – Blog of Viov
After it you'll change the Proxy Context to this:
The only difference here is you need add this line on Header Operations:
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
After change this you will need to create a WebSocket Proxy. OpenLiteSpeed have its own tab for this:
Here you just need to create a WebSocket Proxy, but there are two important things:
After create it you will need to add some Rewrite Rules, if you use CyberPanel this should be done on its website manage area. If you doesn't you just need to go to Rewrite area on OpenLiteSpeed (I'll show on Cyberpanel).
The Rewrite Rule should be like this:
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://[External App Name]/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://[External App Name]/$1 [P,L]
*IMPORTANT*
You'll change the [External App Name] to your External App Name defined during the Reverse Proxy creation, to see the name you'll go to this screen on OpenLiteSpeed:
Here you can see the the name of the External App. After all this you should refresh the OpenLiteSpeed and restart your Asp Net Service. This should solve your problem