Kestrel服务器使用的默认配置文件是hosting.json
.在不同的测试版中,该名称被多次更改.如果您将now project.json
与下面的"command"
部分一起使用
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
}
然后,在从命令行启动服务器的过程中,通过
dnx web
将读取文件hosting.json
.档案
{
"server.urls": "http://0.0.0.0:5000"
}
将服务器配置为在每个IP4地址上侦听5000.配置
{
"server.urls": "http://::5000;http://0.0.0.0:5000"
}
将通知监听5000个IP4和IP6地址.
可以通过使用ASPNET_ENV
环境变量或使用--config myconfig1.json
(或config=myconfig1.json
)来指定替代配置文件.例如,你可以使用
SET ASPNET_ENV=Development
并创建具有特定配置的hosting.Development.json
个文件.或者,您也可以将project.json
与
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
"webProd": "Microsoft.AspNet.Server.Kestrel --config prod.json"
}
并按使用方式启动服务器
dnx webProd
我必须另外提醒您,您可能需要允许额外的收听和注册(开始dnx web
).这是必需的,因为防火墙和监听新TCP/HTTP端口的本地安全性.如下所示应该 for each 人(IPv4和IPv6)进行5000端口的本地注册和侦听:
netsh http add iplisten ipaddress=0.0.0.0:5000
netsh http add iplisten ipaddress=::5000
netsh http add urlacl url=http://+:5000/ user=\Everyone
为了更安全,您可以调整上述配置以授予最低权限.
UPDATED:谢谢@BlaneBunderson.可以使用*代替IP地址(如http://*:5000
)来侦听来自任何接口的any个IP4和IP6地址.你应该小心,不要使用这些东西.
http://*:5000;http://::5000
http://::5000;http://*:5000
http://*:5000;http://0.0.0.0:5000
http://*:5000;http://0.0.0.0:5000
因为它需要注册IP6地址::
或IP4地址0.0.0.0
twice.
对应于the announcement
从技术上讲,任何不是"localhost"或有效IPv4或
我认为这种行为将来可能会改变.因此,我建议只使用*:5000
、0.0.0.0:5000
和::5000
表格注册任何IT地址.
UPDATED 2: ASP.NET Core RC2更改(请参见the announcement)加载默认值的行为.用户必须在Main
中进行更改才能加载来自hosting.json
的设置和命令行参数.下面是一个用法示例
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseUrls("http://*:1000", "https://*:1234", "http://0.0.0.0:5000")
.UseEnvironment("Development")
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
上面的代码使用三个绑定:默认情况下使用"http://*:1000"
、"https://*:1234"
、"http://0.0.0.0:5000"
,而不是默认情况下使用默认端口5000(确切地说,使用http://localhost:5000
)..UseConfiguration(config)
的呼叫被拨打after .UseUrls
.因此,从hosting.json
加载的配置或命令行会覆盖默认选项.如果删除.SetBasePath(Directory.GetCurrentDirectory())
行,则hosting.json
将从编译应用程序DLL的同一目录(例如bin\Debug\netcoreapp1.0
)加载.
可以像这样使用执行
dotnet.exe run --server.urls=http://0.0.0.0:5000
覆盖默认设置(从UseUrls
开始)和hosting.json
的"server.urls"
属性设置(如果存在).
同样,可以通过设置环境变量来覆盖ULR设置
set ASPNETCORE_SERVER.URLS=http://localhost:12541/
然后应用程序的默认启动使用dotnet.exe run
将使用http://localhost:12541/
进行绑定.
您可以找到here个使用HTTPS绑定的示例.
REMARK:在ASP的更高版本中,环境变量的名称从ASPNETCORE_SERVER.URLS
更改为ASPNETCORE_URLS
.NET(请参阅ASP.NET Core 3.1的文档).