gpt4 book ai didi

angular - 将 .NetCore3.1 API 部署到 LinuxCentos(Nginx)

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

我正在尝试将 .NET Core 3.1 API + Angular 部署到它在服务器上运行的 Linux Centos,但浏览器无法正确打开它。如果我为 Windows 发布相同的项目并使用 cmd 运行它,它运行没有问题。当我部署它时,我会根据我在 nginx 配置中的内容得到这种情况:

localhost:5020/weatherforcast - > I get the API's built in json returning functionality.
localhost:5020/Index.html - > I get index html but only <title> loads and *.js and *.css is not found /so blank white screen with bar title/.
localhost:5020 - > I get 404

我也没有任何路由。
这就是我的启动配置方法的样子:
公共(public)无效配置(IApplicationBuilder 应用程序,IWebHostEnvironment env)
{
if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
{
app.UseDeveloperExceptionPage();
}
        app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
{
context.Response.StatusCode = 200;
context.Request.Path = "/index.html";
await next();
}
});

// if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development) app.UseHttpsRedirection();

app.UseRouting();

if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
{
app.UseCors(builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
}
else
{
app.UseStaticFiles();
app.UseDefaultFiles();
}


app.UseAuthentication();
app.UseAuthorization();

app.UseHsts();
app.UseResponseCompression();

app.UseMiddleware<Middlewares.SeederMiddleware>();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<RecievedMessagesHub>("/unread-messages");
endpoints.MapHub<RecipeDetailsTrackHub>("/recipe-details");
});
}

这是我的 Nginx 设置:
server {
listen 80;
server_name app123.tk www.app123.tk;
location / {
proxy_pass https://localhost:5020;
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;
}
}

这就是 Nginx 日志所说的:
[error] 5349#0: *92 connect() failed (111: Connection refused) while   connecting to upstream,    client: X.X.X.X, server: app123.tk, request: "GET  / HTTP/1.1", upstream: "https://127.0.0.1:5020/",  host: "www.app123.tk".

如果我在具有相同 Nginx 设置的同一端口上运行非 Angular 应用程序 MVC,我没有此类问题。

最佳答案

这解决了我的问题:
以下

proxy_set_header  X-Forwarded-Proto $scheme;

我添加了
proxy_max_temp_file_size 0;
proxy_buffering off;

关于angular - 将 .NetCore3.1 API 部署到 LinuxCentos(Nginx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60877108/

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