Error on Asp Net SignalR hosted on Cyberpanel

huangapple go评论56阅读模式
英文:

Error on Asp Net SignalR hosted on Cyberpanel

问题

我正在为农场管理创建一个网站。我在CyberPanel上运行Asp Net Web API。我按照这篇文章完成了这项工作:Publish & Run ASP.Net on CyberPanel / Linux – Blog of Viov

问题是我在使用一个名为SignalR的技术。SignalR基本上是一个顶层的抽象层,用于简化Web套接字(它还包括其他功能,但现在不重要)。在开发环境中一切都按预期进行,但当我部署应用程序并将Asp Net托管在CyberPanel上时,Web套接字停止工作。

Cyberpanel错误日志输出如下:

Error on Asp Net SignalR hosted on Cyberpanel

[2023-05-24 09:26:32.085918 [INFO] [18351] [187.74.137.142:46278#api.milksolve.com.br] 无法找到Web套接字后端。 URI:[/fazendahub]

客户端表示Web套接字未发送握手,因此Web套接字肯定不起作用。我检查了访问日志,在那里我确认Signalr协议在Web套接字连接上中断。有人有解决方案吗?

编辑:

经过一些研究,我发现这个问题与虚拟主机反向代理有关。CyberPanel使用OpenLiteSpeed,而我在互联网上找到的唯一解决方案是针对Ngix和Apache的。我的重写文件如下:

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule /(.*) ws://127.0.0.1:5000/$1 [P]

仍然无法使用WebSocket。

英文:

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:

Error on Asp Net SignalR hosted on Cyberpanel

[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

答案1

得分: 1

The author of the article here, funny that you saved time by my article and I saved time by using yours Error on Asp Net SignalR hosted on Cyberpanel I will update my article with section SignalR & Blazor. Just to make sure can you confirm the name [EXTERNAL SERVER] is always inside [] while doing RewriteRule.

So its

RewriteRule /(.*) ws://[ServerName]/$1 [P,L]

not

RewriteRule /(.*) ws://ServerName/$1 [P,L]

Thanks!

PS:
Soon I will publish an article about direct sync with Github for ASP projects on CyberPanel at my blog. No more suffering with Publish section of visual studio & executing "sudo systemctl restart example.service" for every change.

英文:

The author of the article here, funny that you saved time by my article and I saved time by using yours Error on Asp Net SignalR hosted on Cyberpanel I will update my article with section SignalR & Blazor. Just to make sure can you confirm the name [EXTERNAL SERVER] is always inside [] while doing RewriteRule.

So its

RewriteRule /(.*) ws://[ServerName]/$1 [P,L]

not

RewriteRule /(.*) ws://ServerName/$1 [P,L]

Thanks!

PS:
Soon I will publish an article about direct sync with Github for ASP projects on CyberPanel at my blog. No more suffering with Publish section of visual studio & executing "sudo systemctl restart example.service" for every change.

答案2

得分: 0

以下是翻译好的内容:

在经历了一些困难后,我找到了自己的解决方案。首先,按照这篇文章中的说明设置反向代理:在CyberPanel / Linux上发布和运行ASP.Net – Viov的博客

在此之后,您将更改代理上下文为以下内容:Error on Asp Net SignalR hosted on Cyberpanel

唯一的区别是您需要在标题操作中添加这一行:

RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}

更改后,您需要创建一个WebSocket代理。OpenLiteSpeed有专门的选项卡用于此操作:

Error on Asp Net SignalR hosted on Cyberpanel

在这里,您只需要创建一个WebSocket代理,但有两个重要注意事项:

  1. URI必须为斜杠:/
  2. 地址必须为localhost:port。只有在更改了launchsettings.json文件的情况下才会变化。默认情况下是localhost:5000

创建后,您需要添加一些重写规则,如果使用CyberPanel,则应在其网站管理区域完成此操作。如果没有,请转到OpenLiteSpeed的重写区域(我将在Cyberpanel上展示)。

重写规则应如下所示:

Error on Asp Net SignalR hosted on Cyberpanel

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]

*重要*

您需要将[External App Name]更改为在创建反向代理时定义的External App Name,要查看名称,您需要转到OpenLiteSpeed上的此屏幕:

Error on Asp Net SignalR hosted on Cyberpanel

在这里,您可以看到外部应用程序的名称。完成所有这些步骤后,应刷新OpenLiteSpeed并重新启动您的Asp Net服务。这应该解决您的问题。

英文:

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: Error on Asp Net SignalR hosted on Cyberpanel

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:

Error on Asp Net SignalR hosted on Cyberpanel

Here you just need to create a WebSocket Proxy, but there are two important things:

  1. The URI NEED to be a slash: /
  2. The Address must be localhost:port. It will only change if you changed the launchsettings.json file. By default it is localhost:5000

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:

Error on Asp Net SignalR hosted on Cyberpanel

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:

Error on Asp Net SignalR hosted on Cyberpanel

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

huangapple
  • 本文由 发表于 2023年5月24日 18:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定