You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Ubuntu中用Docker部署SignalR服务器遇CORS错误求助

Docker部署SignalR服务器后CORS连接失败问题解决

问题背景

在Ubuntu环境用Docker部署SignalR服务器,本地未容器化时能正常连接客户端,但容器部署后客户端出现CORS错误,无法建立连接。

相关代码及报错

Program.cs

using chat_server.Hubs;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddCors(p => p.AddPolicy("corsPolicy", build =>
{
    build.WithOrigins("http://[serverIP]:8100", "https://[serverIP]:8100", "http://[serverIP]:80", "https://[serverIP]:80")
    .AllowAnyHeader()
    .AllowAnyMethod()
    .AllowCredentials();
}));

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSignalR();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCors("corsPolicy");

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.MapHub<ChatHub>("/chathub");

app.Run();

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 8100
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["chat_server.csproj", "."]
RUN dotnet restore "./chat_server.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "chat_server.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "chat_server.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "chat_server.dll"]

本地客户端连接代码

const connection = new signalR.HubConnectionBuilder()
            .withUrl("http://[serverIP]:8100/chathub")  // Update with your hub URL
            .build();

控制台报错信息

跨源请求被阻止:同源策略禁止读取位于 https://185.100.232.17:8100/chathub/negotiate?negotiateVersion=1 的远程资源。(原因:CORS 请求未成功)。状态码:(null)

解决方案

1. 修正CORS来源配置

从报错可见,客户端实际发起HTTPS请求,首先要确保CORS策略里的[serverIP]替换为真实服务器IP,同时添加本地客户端的来源(比如http://localhost:xxxx,替换为你的本地客户端端口):

builder.Services.AddCors(p => p.AddPolicy("corsPolicy", build =>
{
    build.WithOrigins(
        "http://185.100.232.17:8100", 
        "https://185.100.232.17:8100", 
        "http://185.100.232.17:80", 
        "https://185.100.232.17:80",
        "http://localhost:xxxx" // 本地客户端地址
    )
    .AllowAnyHeader()
    .AllowAnyMethod()
    .AllowCredentials();
}));

开发环境可临时用.AllowAnyOrigin()测试,但生产环境必须指定具体来源。

2. 调整HTTPS重定向设置

app.UseHttpsRedirection()会将HTTP请求重定向到HTTPS,若容器未配置合法HTTPS证书,会导致请求失败。可先注释该行测试:

// app.UseHttpsRedirection();

注意UseCors必须放在UseHttpsRedirection、UseAuthorization之前,当前代码顺序正确,保持即可。

3. 检查Docker端口映射

运行容器时需确保主机端口与容器端口正确映射:

# HTTP端口映射
docker run -p 8100:8100 -d your-image-name

# 若启用HTTPS,需同时映射443端口
docker run -p 8100:8100 -p 443:443 -d your-image-name

4. 匹配客户端连接协议

客户端代码用的是http://,但报错显示请求走了https://,需将客户端连接URL改为HTTPS:

const connection = new signalR.HubConnectionBuilder()
            .withUrl("https://185.100.232.17:8100/chathub")
            .build();

内容的提问来源于stack exchange,提问作者Mir Injamamul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:04:57