【问题标题】:Deploying .NetCore3.1 API to LinuxCentos(Nginx)将 .Net Core 3.1 API 部署到 Linux Centos(Nginx)
【发布时间】:2020-07-07 16:00:59
【问题描述】:

我正在尝试将 .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

我也没有任何路由。 这就是我的启动配置方法的样子: 公共无效配置(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,则不会出现此类问题。

【问题讨论】:

    标签: angular api nginx deployment centos


    【解决方案1】:

    这解决了我的问题: 下面

    proxy_set_header  X-Forwarded-Proto $scheme;
    

    我加了

    proxy_max_temp_file_size 0;
    proxy_buffering off;
    

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多