2
votes

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

1
Are you serving this to the domain via a proxy server? You need to make sure web socket connections can make it through. - Andrei Tătar
I am using Virtual host on server. I connect to demo.bkit.vn not via a proxy. - Do Nhu Vy
What is this virtual host? I assume it's still some kind of proxy that fwds the requests to your app based on the domain. Maybe look into the documentation if it supports web sockets. - Andrei Tătar
This virtual host point Blazor webapp from http://localhost:5000 on server to sub-domain demo.bkit.vn for publishing. - Do Nhu Vy

1 Answers

4
votes

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