【问题标题】:Kestrel :: Bind to multiple HTTP and HTTPS urls through appsettings.jsonKestrel :: 通过 appsettings.json 绑定到多个 HTTP 和 HTTPS url
【发布时间】:2021-11-10 19:40:03
【问题描述】:

我在 .Net 5 中构建了一个 ASP.NET Core 服务,它使用 Kestrel 作为边缘服务器。该服务需要在 HTTP 协议上侦听多个域 - 例如http://foo:12912 & http://bar:12912 和 HTTPS 协议,如 http://foo:443 & http://bar:443

我知道我们可以在Program.cs 中使用UseUrls

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>();
                webBuilder.UseUrls("http://foo:12912", "http://bar:12912");
            });
}

但是 - 鉴于 Kestel 已经发展了如此之多 - 应该有一种方法可以从 json 配置本身进行配置。我能找到的所有配置示例仅适用于单个 url,例如

"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://foo:12912"
    },
    "Https": {
      "Url": "https://foo:443"
    }
  }
}

属性名称也是url 而不是urls 所以我的假设是它只支持单个url 而不是多个。我的期望是正确的还是我错过了什么?

【问题讨论】:

    标签: c# asp.net-core c#-5.0 kestrel-http-server kestrel


    【解决方案1】:

    在开发中,您可以像这样使用 launchSettings.json:

    "WebApplication1": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
    

    请注意,applicationUrl 采用分号分隔的列表。

    在生产中,通常红隼会在前面放置一个反向代理。

    就像配置为反向代理的 NGINX 一样,它可以处理 SSL,而 kestrel 不需要公开 ssl 端口,因为所有安全应该由实际的网络服务器处理,强>红隼。

    Kestrel 并非设计为暴露到互联网。但它实际上是 Web 应用程序的简约包装器。

    话虽这么说...... 但是,有办法做你想做的事。

    方式 1 使用多个端点: 请注意,您可以添加许多具有不同 json 键的端点。

     {
      "Kestrel": {
        "Endpoints": {
          "Http": {
            "Url": "http://localhost:5000"
          },
          "HttpsInlineCertFile": {
            "Url": "https://localhost:5001",
            "Certificate": {
              "Path": "<path to .pfx file>",
              "Password": "<certificate password>"
            }
          },
          "HttpsInlineCertAndKeyFile": {
            "Url": "https://localhost:5002",
            "Certificate": {
              "Path": "<path to .pem/.crt file>",
              "KeyPath": "<path to .key file>",
              "Password": "<certificate password>"
            }
          },
          "HttpsInlineCertStore": {
            "Url": "https://localhost:5003",
            "Certificate": {
              "Subject": "<subject; required>",
              "Store": "<certificate store; required>",
              "Location": "<location; defaults to CurrentUser>",
              "AllowInvalid": "<true or false; defaults to false>"
            }
          },
          "HttpsDefaultCert": {
            "Url": "https://localhost:5004"
          }
        },
        "Certificates": {
          "Default": {
            "Path": "<path to .pfx file>",
            "Password": "<certificate password>"
          }
        }
      }
    }
    

    方式 2 使用 ASPNETCORE_URLS 或 DOTNET_URLS 环境变量并分配以分号分隔的 url 列表。

    方式 3 在 appsettings.json 中设置“urls”键,该键也使用分号分隔的 url 列表。

    {
      "Urls": "http://localhost:9999;https://someotherdomain.com"  
    }
    

    每个方法的更多文档和限制在这里

    Configure endpoints for the ASP.NET Core Kestrel web server

    【讨论】:

    • 谢谢。对于方式 3 - 你可以发布整个配置 - Urls 以及 Kestrel 标签吗?
    • @NikhilAgrawal 方式 3 不在红隼标签内。它位于 appsettings.json 的根目录,并且遵循相同的分号分隔的 url 列表模式。我添加了另一个网址,以便您了解我的意思
    【解决方案2】:

    基本上,您只需在 Url 部分中添加分号 下面是https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/的摘录

    {
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:38327",
          "sslPort": 44310
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "TestApp": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2012-11-16
      • 1970-01-01
      • 2016-07-11
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多