如何配置nginx与SignalR asp.net Core?



我最近遇到了一个问题,不得不花很多时间来配置它。

让我来告诉你问题所在。

我试图使用SignalR,但它在本地工作,但不是在服务器上使用Nginx。

下面是我的代码:

歌珥政策:

services.AddCors(options =>
{
options.AddPolicy("DevCorsPolicy", builder => builder
.WithOrigins("http://localhost:4200", "https://localhost:4200", "http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
options.AddPolicy("ProdCorsPolicy", builder => builder
.WithOrigins(
"http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
});

SignalR:

//this is in ConfigureServices
services.AddSignalR();
//this is in Configure
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<NotificationHub>("/notification");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");

});

前端:

start() {
let token = 'myjwttoken';
let url = 'https://localhost:5001/notification';
const connection = new HubConnectionBuilder()
.withUrl(url, {
accessTokenFactory: () => {
return token;
},
})
.configureLogging(LogLevel.Information)
.build();
connection
.start()
.then(() => {
console.log('Connected');
})
.catch(err => {
console.log('Disconnected');
console.log(err);
});
}

现在可以在本地工作了,没有任何问题。

因此,为了使其在Server Prod/Dev环境EC2或VM或任何Linux服务器上工作。

server {
server_name   api.com;
location / {
proxy_pass         http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header   Upgrade $http_upgrade;
proxy_set_header   Connection keep-alive;
proxy_set_header   Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header   X-Forwarded-Proto $scheme;
}
location /notification {
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;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header   X-Forwarded-Proto $scheme;
}
}

相关内容

  • 没有找到相关文章

最新更新