Vy Do
Vy Do

Reputation: 52656

ASP.NET Core 3 Blazor: Run on localhost of server ok, but run outside server error: Error during WebSocket handshake: Unexpected response code: 200

I am using .NET Core 3.0.0 , Blazor server-side, Visual Studio 2019, Windows Server 2016. I run on server ok, but connection from outside server is error.

I use Apache HTTP Web server 2.4 virtual host (http://httpd.apache.org/docs/2.4/vhosts/ ), file httpd-vhosts.conf

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ServerName demo.example.com
    ErrorLog ${APACHE_LOG_DIR}webapp1-error.log
    CustomLog ${APACHE_LOG_DIR}webapp1-access.log common
</VirtualHost>

In httpd.conf , enable module: proxy , proxy_httpd , proxy_wstunnel and

<VirtualHost *:80>
  ServerName demo.example.com

  RewriteEngine On
  RewriteCond %{REQUEST_URI}  ^/demo.example.com            [NC]
  RewriteCond %{QUERY_STRING} transport=websocket    [NC]
  RewriteRule /(.*)           ws://localhost:5000/$1 [P,L]

  ProxyPass / http://localhost:5000/
  ProxyPassReverse / http://localhost:5000/
</VirtualHost>

this error

blazor.server.js:1 WebSocket connection to 'ws://demo.xxxx.xx/_blazor?id=LZk17pmIejWGHLkhN3HFmA' failed: Error during WebSocket handshake: Unexpected response code: 200

Upvotes: 2

Views: 3806

Answers (1)

Andrei Tătar
Andrei Tătar

Reputation: 8295

Seems others had problems with proxying the web socket connection. Found a potential solution for you here: https://github.com/aspnet/Blazor/issues/1882

<VirtualHost *:80>
ServerName www.domain.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/

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

ErrorLog /.log
CustomLog /.log common

Upvotes: 4

Related Questions