所以我有一個容器化的應用程式,它由 react asp.net core keycloak nginx 組成。
我不想將 API 的埠發布到互聯網,我只希望它可以在容器的網路中訪問。
但我無法從 React 應用程式進行 API 呼叫。
現在我在這里做他們:
https://localhost:7266
docker-compose 的 asp.net 核心容器
mediere-api:
container_name: best-asp.net
image: ${DOCKER_REGISTRY-}mediere-api
build:
context: ..
dockerfile: Dockerfile
env_file:
- .env
depends_on:
- db
expose:
- "7266"
volumes:
- ../certs/certificate.pfx:/etc/https/certs
它的dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["mediere-API.csproj", "."]
RUN dotnet restore "mediere-API.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "mediere-API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "mediere-API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "mediere-API.dll"]
應用設定.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
deleted
}
}
啟動設定.json
{
"profiles": {
"mediere_API": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "https://localhost:7266",
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true,
"useSSL": true
}
}
}
.env
#ASP.Net core
ASPNETCORE_STATICWEBASSETS="/app/bin/Debug/net6.0/mediere-api.staticwebassets.runtime.CT.json"
ASPNETCORE_ENVIRONMENT=Production
ASPNETCORE_URLS=https://localhost:7266
程式.cs
using mediere_API.DataLayer;
using mediere_API.DataLayer.Repository.Implementations;
using mediere_API.DataLayer.Repository.Interfaces;
using mediere_API.Extensions;
using mediere_API.Middleware;
using mediere_API.Processors.Implementations;
using mediere_API.Processors.Interfaces;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
//Servicii
builder.Services.AddControllers();
builder.Services.ConfigureSwagger();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddRepositories();
builder.Services.AddProcessors();
//UnitsOfWork
builder.Services.AddScoped<ISchemaUnitOfWork, SchemaUnitOfWork>();
builder.Services.AddScoped<IMainUnitOfWork, MainUnitOfWork>();
//AutoMapper
builder.Services.AddAutoMapper(typeof(Program));
//Keycloak
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
{
o.MetadataAddress = "https://keycloak:8443/auth/realms/best-realm/.well-known/openid-configuration";
o.Authority = "https://keycloak:8443/auth/realms/best-realm";
o.Audience = "account";
o.RequireHttpsMetadata = false;
});
//CORS
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(
policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.WithExposedHeaders("FileName");
});
});
builder.Services.AddDbContext<EfDbContext>();
builder.Services.AddDbContext<SchemaEfDbContext>();
var app = builder.Build();
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseMiddleware<SchemaHandlingMiddleware>();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();
使用這些設定,我的 API 呼叫似乎無法到達 Docker 容器。我得到:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:7266/api/proiecte. (Reason: CORS request did not succeed). Status code: (null)
狀態代碼 null 意味著我認為無法到達容器。
那我的設定有什么問題嗎?
我應該如何從 docker 對 ASP 容器進行 API 呼叫以使其正常作業?
謝謝。
uj5u.com熱心網友回復:
使用反向代理的同源服務方法:
您應該在您的 nginx 配置中反向代理您的 API。像這樣的東西:
nginx.conf:
位置/proxy_pass:TO_YOUR_REACT_APP 或:SERVER_YOUR_REACT_APP_STATIC_FILES
位置 /api proxy_pass: TO_YOUR_API
這種方法使您的 API 和 REACT 應用程式處于同一來源。所以不會有CORS錯誤。
為您通過瀏覽器發送的請求啟用 CORS。請分享您的瀏覽器控制臺錯誤的螢屏截圖,以便幫助您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511728.html
