我有一個在.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"/span>, "http://bar:12912"/span>)。
});
}
然而--鑒于Kestel已經發展了這么多--應該有一種方法可以從json配置本身來配置這個。我所找到的所有配置例子都只針對一個單一的URL,比如
。"Kestrel"/span>: {
"EndPoints"/span>: {
"Http": {
"Url": "http://foo:12912"。
},
"Https"/span>: {
"Url": "https://foo:443"。
}
}
}
屬性名稱也是url而不是urls,所以我的假設是它只支持單個url而不支持多個。我的期望是正確的嗎?還是我錯過了什么?
uj5u.com熱心網友回復:
基本上,你只需要在你的Url部分加上分號即可。 下面是https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/的摘錄。
{
"iisSettings"/span>: {
"windowsAuthentication": false,
"anonymousAuthentication": true。
"iisExpress": {
"applicationUrl": "http://localhost:38327",
"sslPort": 44310.
}
},
"組態檔": {
"IIS Express"/span>: {
"commandName": "IISExpress",
" launchBrowser": true。
"environmentalVariables": {
"ASPNETCORE_ENVIRONMENT": "Development": "ASPNETCORE_ENVIRONMENT""TestApp": {
"commandName": "Project",
" launchBrowser": true。
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentalVariables": {
"ASPNETCORE_ENVIRONMENT": "Development": "ASPNETCORE_ENVIRONMENT"
uj5u.com熱心網友回復:
在開發中,你可以像這樣使用 launchSettings.json:
請注意,applicationUrl需要一個由分號分隔的串列。
在生產中,通常kestrel將由一個反向代理來支撐。
比如NGINX配置為反向代理,它將處理SSL,而Kestrel將不需要暴露SSL埠,因為所有的安全應該由實際的Webserver處理,而不是由Kestrel處理。
Kestrel不是被設計成暴露在互聯網上。但它實際上是一個網路應用程式的最小化包裝。 這就是說,。
話雖如此,....
然而,有一種方法可以實作你想要的東西。
方法一:使用多個端點。
請注意,你可以用不同的json鍵添加許多端點。
方法 2 使用 ASPNETCORE_URLS 或 DOTNET_URLS 環境變數并指定一個分號分隔的 urls 串列。
方法3在appsettings.json中設定 "urls "鍵,也使用分號分隔的urls串列。
每一種方法的進一步檔案和限制在這里 為ASP.NET Core Kestrel網路服務器配置端點
標籤:"WebApplication1"/span>: {
"commandName": "Project",
" launchBrowser": true。
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentalVariables": {
"ASPNETCORE_ENVIRONMENT": "Development": "ASPNETCORE_ENVIRONMENT"
{
"Kestrel"/span>: {
"Endpoints"/span>: {
"Http": {
"Url": "http://localhost:5000"。
},
"HttpsInlineCertFile": {
"Url": "https://localhost:5001",
"證書": {
"Path": "<path to .pfx file>",
"密碼": "<證書密碼>", "密碼": "<證書密碼>"
}
},
"HttpsInlineCertAndKeyFile"/span>: {
"Url"/span>: "https://localhost:5002",
"證書": {
"Path": "<path to .pem/.crt file>"/span>,
"KeyPath": "<path to .key file>",
"密碼": "<證書密碼>", "密碼": "<證書密碼>"
}
},
"HttpsInlineCertStore"/span>: {
"Url": "https://localhost:5003",
"證書": {
"主題": "<subject; required>"。
"商店": "<certificate store; required>"。
"Location": "<location; defaults to CurrentUser>"。
"AllowInvalid": "<true or false; defaults to false>".
}
},
"HttpsDefaultCert": {
"Url"/span>: "https://localhost:5004"。
}
},
"證書": {
"默認": {
"路徑": "<path to .pfx file>",
"密碼": "<證書密碼>", "密碼": "<證書密碼>"
}
}
}
{
"Urls"/span>: "http://localhost:9999;https://someotherdomain.com"。
}
