Nginx之后的ASP.NET Core中的SignalR [英] SignalR in ASP.NET Core behind Nginx

查看:152
本文介绍了Nginx之后的ASP.NET Core中的SignalR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台具有ubuntu 16.04,kestrel和nginx的服务器作为代理服务器,该服务器重定向到我的应用程序所在的本地主机.我的应用程序位于Asp.Net Core 2上.我试图添加推送通知并使用SignalR core.在localhost上,一切运行良好,并且在带有iis和Windows的免费托管上也是如此.但是,当我在Linux服务器上部署应用程序时,出现错误:

I have a server with ubuntu 16.04, kestrel and nginx as a proxy server that redirects to localhost where my app is. And my app is on Asp.Net Core 2. I'm trying to add push notifications and using SignalR core. On localhost everything is working well, and on a free hosting with iis and windows as well. But when I deploy my app on the linux server I have an error:

signalr-clientES5-1.0.0-alpha2-final.min.js?v = kyX7znyB8Ce8zvId4sE1UkSsjqo9gtcsZb9yeE7Ha10:1 WebSocket连接到 'ws://devportal.vrweartek.com/chat?id = 210fc7b3-e880-4d0e-b2d1-a37a9a982c33' 失败:WebSocket握手期间出错:意外的响应代码: 204

signalr-clientES5-1.0.0-alpha2-final.min.js?v=kyX7znyB8Ce8zvId4sE1UkSsjqo9gtcsZb9yeE7Ha10:1 WebSocket connection to 'ws://devportal.vrweartek.com/chat?id=210fc7b3-e880-4d0e-b2d1-a37a9a982c33' failed: Error during WebSocket handshake: Unexpected response code: 204

但是仅当我通过站点名称从其他计算机请求站点时,才会发生此错误.当我通过localhost:port从服务器请求站点时,一切都很好.因此,我认为nginx中存在问题.我阅读我需要对其进行配置,以与Signalr中使用的websocket一起使用.建立联系,但我没有成功.可能只是一些愚蠢的错误?

But this error occurs only if I request my site from different machine via my site name. And when I request the site from the server via localhost:port everything is fine. So I think there is a problem in nginx. I read that I need to configure it for working with websockets which are used in signalr for establishing connection but I wasn't succeed. May be there is just some dumb mistake?

推荐答案

问题是nginx配置文件.如果您使用的是ASP.NET Core部署指南的默认设置,则问题是代理标头之一. WebSocket要求Connection标头为"upgrade".

The problem is the nginx configuration file. If you are using the default settings of the ASP.NET Core deployment guide then the problem is the one of the proxy headers. WebSocket requires Connection header as "upgrade".

您必须在nginx配置文件上为SignalR Hub设置新路径.

You have to set a new path for SignalR Hub on nginx configuration file.

例如

location /api/chat {
    proxy_pass http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

您可以阅读我的完整博客文章

You can read my full blog post

https://medium.com/@alm. ozdmr/信号部署与nginx-daf392cf2b93

这篇关于Nginx之后的ASP.NET Core中的SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆