首页 > 解决方案 > 使用 Nginx 在 Linux 上托管 ASP.NET Core - 在 LAN 上无法访问站点

问题描述

我正在尝试在 Linux 上访问 ASP .net Core 3 Web API(本例为树莓派)。

该 API 在 localhost 中完美运行,无论是在我的笔记本电脑上还是在本地访问时在 pi 上。但是,当我尝试让它在ASP DOCS之后的计划中可用时,它不起作用。

如果我让 dotnet 应用程序运行,我会得到以下信息:

“此站点无法访问 192.168.1.50 拒绝连接。尝试:

检查连接 检查代理和防火墙 ERR_CONNECTION_REFUSED" "

如果 dotnet 应用程序未运行并且我尝试访问 Nginx 服务器,我会得到以下信息:

502错误的网关

NGINX 似乎正在做一些事情,因为运行它的应用程序将它转发到正确的端口 5001

我的程序.cs:

 public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

我的启动.cs

  public class Startup
{

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
       var con = new ConnectionStrings();
       Configuration.Bind("ConnectionStrings", con);
       services.AddSingleton(con);
       services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

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


        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

来自 Linux 的 dotnet:

    dotnet Fan.dll 
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/pi/WebApp

Linux Nginx 状态/配置:

在此处输入图像描述 在此处输入图像描述

标签: c#asp.netlinuxnginx

解决方案


推荐阅读