gpt4 book ai didi

asp.net-core - 将 Letsencrypt SSL 添加到 nginx 后 SignalR 失败

转载 作者:行者123 更新时间:2023-12-04 12:02:00 47 4
gpt4 key购买 nike

我为 SignalR 创建了一个小型演示项目。当我在没有 SSL 的情况下在 Nginx 后面发布我的项目时,这一切都奏效了。但是在我运行 Certbot 并将 SSL 添加到站点后,所有 SignalR 集线器/WebSockets 都停止工作。该网站可以在这里找到:
https://app.topswagcode.com

signalr.js:5053 WebSocket connection to 'wss://app.topswagcode.com/chatHub?id=v_UmTZyB9v962n44Wf2-Kg' failed: 
(anonymous) @ signalr.js:5053
(anonymous) @ signalr.js:5034
step @ signalr.js:4995
(anonymous) @ signalr.js:4976
(anonymous) @ signalr.js:4970
__awaiter @ signalr.js:4966
WebSocketTransport.connect @ signalr.js:5016
HttpConnection.startTransport @ signalr.js:4149
(anonymous) @ signalr.js:4099
step @ signalr.js:3710
(anonymous) @ signalr.js:3691
(anonymous) @ signalr.js:3685
__awaiter @ signalr.js:3681
HttpConnection.createTransport @ signalr.js:4052
(anonymous) @ signalr.js:3970
step @ signalr.js:3710
(anonymous) @ signalr.js:3691
fulfilled @ signalr.js:3682
signalr.js:2150 [2021-06-11T06:43:20.004Z] Error: Failed to start the transport 'WebSockets': Error: There was an error with the transport.
我的 API 端点仍然有效,而且 SSL 似乎在一般情况下有效。
为了部署我的应用程序,我遵循: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-5.0
对于正常的 HTTP 连接运行顺利。 Certbot 添加了它的 SSL 内容并破坏了我的 WebSockets :(

编辑
所以我试图恢复所有更改并重新开始。我没有将所有流量重定向到 SSL(我应该这样做),而是同时拥有 HTTP 和 HTTPS
http://app.topswagcode.com/Stock
https://app.topswagcode.com/Stock
我的 Http 套接字仍然有效,但 HTTPS 无效。
我的 Nginx 设置:
server { 
server_name app.topswagcode.com;
location / {
proxy_pass http://127.0.0.1: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 /chathub {
proxy_pass http://127.0.0.1:5000/chathub;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
}


listen 80; # managed by Certbot

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/app.topswagcode.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/app.topswagcode.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
我尝试添加在一些帖子中找到的聊天中心部分。但是没有为 SSL 做任何事情。
API 端点仍然适用于 HTTP 和 HTTPS。仍然对我做错了什么感到失落。很可能在 Nginx 配置中很简单。
为此,我也将在下面分享我的 Startup.cs。 id 没什么特别的。我已经禁用了 app.UseHttpsRedirection();和 app.UseHsts();
    public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddControllers();
services.AddSignalR();

services.AddTransient<IStockService, FakeStockService>();
services.AddHostedService<TimedHostedService>();
services.AddHostedService<StockHostedService>();
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
//app.UseHsts();
}

//app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});


app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chathub");
endpoints.MapHub<ProcessHub>("/processhub");
endpoints.MapHub<GraphHub>("/graphhub");
endpoints.MapHub<StockHub>("/stockhub");
});
}
编辑 2
所以我刚刚发现了这个:
enter image description here
enter image description here
此请求仅对 Https 失败。请求正文是:
{"protocol":"json","version":1}
但是对于 HTTPS,它只是返回:未找到“没有与该 ID 的连接”

最佳答案

在 Startup.js/ConfigureServices 中:

services.AddSignalR(options => {
​options.KeepAliveInterval = TimeSpan.FromSeconds(15);
​options.EnableDetailedErrors = true;
​});
在 Startup.js/配置中:
app.UseWebSockets();
app.UseStaticFiles();

关于asp.net-core - 将 Letsencrypt SSL 添加到 nginx 后 SignalR 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67932469/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com