如何让 kestrel 网络服务器监听非本地主机请求?
How do I get the kestrel web server to listen to non-localhost requests?
我已经将我的 c#、asp.net 5、mvc 6 应用程序部署到 windows 2008 服务器。我已经启动 dnx web
,它正在侦听端口 5000,并且在从本地计算机访问时工作正常。
如何让它监听非本地主机请求?
P.S。这个 不是这个的副本...它指的是 asp.net pre RC1 而 hosting.ini 实际上有一个 .ini 格式。现在,它是 JSON,我找不到任何关于它实际应该包含什么的文档。
P.P.S。真正的解决方案是在链接问题的未接受 中,有一个巨大的警告。步骤:
- 根据链接的答案更改您的 project.json。
- 将您的项目发布到您的服务器。
- 在服务器上,转到...\approot\src\YourProject 文件夹并在那里打开命令 window。
- 运行
dnx web
- 它会失败
- 运行
dnu restore
- 运行 'dnu build`
- 运行 'dnx web` - Web 服务器现在应该可以正常启动
P.S。对于提出这个问题的人。它已经过时了。非常过时!
它适用于.NET Core 的早期版本。问题和答案当然不适用于框架的当前版本(例如 2.x、3.x)
Kestrel 服务器使用的默认配置文件是 hosting.json
。该名称在不同的测试版中多次更改。如果您现在将 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"
}
将通知在 IP4 和 IP6 地址上监听 5000。
可以通过使用 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 端口的本地安全性。像下面这样的东西应该为每个人(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
为了更安全,您可以调整上述配置以授予最小权限。
更新:谢谢@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
两次.
Technically, any hostname that isn't "localhost" or a valid IPv4 or
IPv6 address will cause Kestrel to bind to all network interfaces.
我认为将来可以改变这种行为。因此,我建议仅使用 *:5000
、0.0.0.0:5000
和 ::5000
表格来注册任何 IT 地址。
更新 2: ASP.NET 核心 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)
的调用在 .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.NET的后续版本中由ASPNETCORE_SERVER.URLS
改为ASPNETCORE_URLS
(见here ASP.NET Core 3.1) 的文档。
在 RC2 中,不再使用 project.json 的命令部分。我还没有让 Kestrel 选择 hosting.json,但是您可以通过编程方式在创建和配置新 WebHostBuilder 的应用程序的 Main 中设置端口。只需像下面的示例一样添加 .UseUrls() 方法
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseUrls("http://0.0.0.0:5000/")
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
如果您尝试将 ASP.NET 核心应用程序放入 docker 容器中(这是我需要侦听非本地主机地址的用例),请注意此用例具有Microsoft 已经为您准备好了。你可以在https://hub.docker.com/r/microsoft/aspnetcore/
看到完整的荣耀
当前 (v1.0.1) 解决此问题的关键在于源 Dockerfile 包含一个 url 环境变量设置,并且应用程序不会尝试覆盖它。 (事实上 ,容器化应用程序应该在内部尽可能少地断言它将运行的环境。)
ENV ASPNETCORE_URLS http://+:80
注意加号而不是星号。只要 link 是好的,我实际上建议访问上面的 dockerhub link 而不是阅读我的答案。 1.1版本在即,未来可能会再次发生变化。
当 运行 连接容器时,确保根据环境变量设置公开来宾端口 80。例如:
docker run -d -p 8000:80 myapp
curl localhost:8000
如果你使用asp.net核心2.1+,修改appsettings.json中的配置部分。
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5002"
}
}
},
将环境变量 ASPNETCORE_URLS
设置为 http://0.0.0.0:5000/
。
如果 运行 来自 Visual Studio,则从项目属性的“调试”选项卡添加环境变量。
对于 AspNetCore 3.1+ 只需在文件中添加以下行 appsettings.json
:
"Urls": "http://*:80"
另一种选择是修改launchSettings.json
。将 "applicationUrl": "http://localhost:5000",
替换为 "applicationUrl": "http://0.0.0.0:5000",
。像这样:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3031"
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication1": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://0.0.0.0:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
我已经将我的 c#、asp.net 5、mvc 6 应用程序部署到 windows 2008 服务器。我已经启动 dnx web
,它正在侦听端口 5000,并且在从本地计算机访问时工作正常。
如何让它监听非本地主机请求?
P.S。这个
P.P.S。真正的解决方案是在链接问题的未接受
- 根据链接的答案更改您的 project.json。
- 将您的项目发布到您的服务器。
- 在服务器上,转到...\approot\src\YourProject 文件夹并在那里打开命令 window。
- 运行
dnx web
- 它会失败 - 运行
dnu restore
- 运行 'dnu build`
- 运行 'dnx web` - Web 服务器现在应该可以正常启动
P.S。对于提出这个问题的人。它已经过时了。非常过时!
它适用于.NET Core 的早期版本。问题和答案当然不适用于框架的当前版本(例如 2.x、3.x)
Kestrel 服务器使用的默认配置文件是 hosting.json
。该名称在不同的测试版中多次更改。如果您现在将 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"
}
将通知在 IP4 和 IP6 地址上监听 5000。
可以通过使用 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 端口的本地安全性。像下面这样的东西应该为每个人(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
为了更安全,您可以调整上述配置以授予最小权限。
更新:谢谢@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
两次.
Technically, any hostname that isn't "localhost" or a valid IPv4 or IPv6 address will cause Kestrel to bind to all network interfaces.
我认为将来可以改变这种行为。因此,我建议仅使用 *:5000
、0.0.0.0:5000
和 ::5000
表格来注册任何 IT 地址。
更新 2: ASP.NET 核心 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)
的调用在 .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.NET的后续版本中由ASPNETCORE_SERVER.URLS
改为ASPNETCORE_URLS
(见here ASP.NET Core 3.1) 的文档。
在 RC2 中,不再使用 project.json 的命令部分。我还没有让 Kestrel 选择 hosting.json,但是您可以通过编程方式在创建和配置新 WebHostBuilder 的应用程序的 Main 中设置端口。只需像下面的示例一样添加 .UseUrls() 方法
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseUrls("http://0.0.0.0:5000/")
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
如果您尝试将 ASP.NET 核心应用程序放入 docker 容器中(这是我需要侦听非本地主机地址的用例),请注意此用例具有Microsoft 已经为您准备好了。你可以在https://hub.docker.com/r/microsoft/aspnetcore/
看到完整的荣耀当前 (v1.0.1) 解决此问题的关键在于源 Dockerfile 包含一个 url 环境变量设置,并且应用程序不会尝试覆盖它。 (事实上 ,容器化应用程序应该在内部尽可能少地断言它将运行的环境。)
ENV ASPNETCORE_URLS http://+:80
注意加号而不是星号。只要 link 是好的,我实际上建议访问上面的 dockerhub link 而不是阅读我的答案。 1.1版本在即,未来可能会再次发生变化。
当 运行 连接容器时,确保根据环境变量设置公开来宾端口 80。例如:
docker run -d -p 8000:80 myapp
curl localhost:8000
如果你使用asp.net核心2.1+,修改appsettings.json中的配置部分。
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5002"
}
}
},
将环境变量 ASPNETCORE_URLS
设置为 http://0.0.0.0:5000/
。
如果 运行 来自 Visual Studio,则从项目属性的“调试”选项卡添加环境变量。
对于 AspNetCore 3.1+ 只需在文件中添加以下行 appsettings.json
:
"Urls": "http://*:80"
另一种选择是修改launchSettings.json
。将 "applicationUrl": "http://localhost:5000",
替换为 "applicationUrl": "http://0.0.0.0:5000",
。像这样:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3031"
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication1": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://0.0.0.0:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}