繁体   English   中英

ASP.NET 核心:使用 Kestrel 监听 `https://mydomain` 和 `https://www.mydomain`

[英]ASP.NET Core: Listening on both `https://mydomain` and `https://www.mydomain` with Kestrel

我正在尝试使用 Kestrel 设置 ASP.NET Core web 服务器。 I want Kestrel to listen for all requests coming from both https://www.example.com and https://example.com , but I can't quite figure out how to do this.

我目前正在使用以下代码在我的程序启动中设置 Kestrel:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<AppStartup>();
            webBuilder.UseUrls(
                "https://example.com:443",
                "http://example.com:80",
                //"https://www.example.com:443",        // This obviously doesn't work with all enabled, 
                //"http://www.example.com:80",          // as it tries to double-bind the ports...
            );
            // Workaround for HTTP2 bug in .NET Core 3.1 and Windows 8.1 / Server 2012 R2
            webBuilder.UseKestrel(options =>
            options.ConfigureEndpointDefaults(defaults =>
                defaults.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1
            )
        );
    });

我还尝试在我的AppStartup中执行以下操作,我真的认为这是解决方案,离开了这个资源。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.UseRewriter(opt => opt.AddRedirectToWwwPermanent());
    ...
}

但这似乎不起作用。 通过 Kestrel 执行此操作的正确方法是什么? 一些预期的问题:

不,我没有在 IIS 后面运行 Kestrel 并且无意这样做(不太清楚为什么我一直看到人们这样做?我应该这样做吗?我能从中得到什么?)

非常感谢您的阅读!

默认情况下,ASP.NET Core 绑定到:

使用以下方式指定 URL:

  • ASPNETCORE_URLS 环境变量。
  • --urls 命令行参数。
  • urls 主机配置密钥。
  • UseUrls 扩展方法。

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#endpoint-configuration

在 launchsettings.json 你可能有"applicationUrl": "https://localhost:5001;http://localhost:5000"这是它在调试过程中的完成方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM