如何让 kestrel Web 服务器监听非本地主机请求?

我已经将我的 c # ,asp.net 5,mvc 6应用程序部署到了一个 Windows 2008服务器上。我已经启动了 dnx web,它正在监听端口5000,从本地计算机访问时工作正常。

如何让它监听非本地主机请求?

附言。这个 有个问题不是这个的副本... 它引用 asp.net pre RC1,而 hosting.ini 实际上有一个。Ini 格式。现在,它是 JSON,我找不到任何关于它实际应该包含什么的文档。

附注: 真正的解决方案是在不被接受的 回答中对相关问题加上一个大大的警告。步骤:

  1. 根据链接的答案更改您的项目.json。
  2. 将项目发布到服务器。
  3. 在服务器上,转到... provot src YourProject 文件夹并在那里打开一个命令窗口。
  4. 运行 dnx web-它将失败
  5. 运行 dnu restore
  6. 运行‘ dnu build’
  7. 运行‘ dnx web’-web 服务器现在应该可以正常启动了

附注: 给那些支持这个问题的人的,这个问题已经过时了,非常过时!

它适用于。NET 核心。问题和答案当然不适用于当前版本的框架(例如2.x,3.x)

121206 次浏览

The default configuration file used by Kestrel server is hosting.json. The name was changed multiple times in different beta versions. If you use now project.json with the following "command" section

"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
}

then during starting the server from the command line by

dnx web

the file hosting.json will be read. The file

{
"server.urls": "http://0.0.0.0:5000"
}

will configure the server to listen 5000 on every IP4 address. The configuration

{
"server.urls": "http://::5000;http://0.0.0.0:5000"
}

will inform to listen 5000 on both IP4 and IP6 address.

One can specify alternative configuration files by usage ASPNET_ENV environment variable or by the usage of --config myconfig1.json (or config=myconfig1.json). For example you can use

SET ASPNET_ENV=Development

and to create hosting.Development.json file with specific configuration. Alternatively you can use project.json with

"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
"webProd": "Microsoft.AspNet.Server.Kestrel --config prod.json"
}

and start the server by usage

dnx webProd

I have to remind additionally that it could be required that you allow to additionally listen and to register (to start dnx web). It's required because of the firewall and the local security of listening new TCP/HTTP ports. Something like below should make local registering and listening of 5000 port for everybody (IPv4 and IPv6):

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

To be more secure you can adjust the above configuration to grant minimal rights.

UPDATED: Thanks @BlaneBunderson. One can use * instead of IP address (like http://*:5000) to listen on any IP4 and IP6 addresses from any interface. One should be carefully and not use these

  • http://*:5000;http://::5000
  • http://::5000;http://*:5000
  • http://*:5000;http://0.0.0.0:5000
  • http://*:5000;http://0.0.0.0:5000

because it will require to register IP6 address :: or IP4 address 0.0.0.0 twice.

Corresponds to the announcement

Technically, any hostname that isn't "localhost" or a valid IPv4 or IPv6 address will cause Kestrel to bind to all network interfaces.

I think that the behavior could be changed in the future. Thus I would recommend to use only *:5000, 0.0.0.0:5000 and ::5000 form for registering of any IT address.

UPDATED 2: ASP.NET Core RC2 changes (see the announcement) the behavior of loading the defaults. One have to make changes in the Main to load the settings from hosting.json and the command line parameters. Below is an example of the usage

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();
}

The above code use three bindings: "http://*:1000", "https://*:1234", "http://0.0.0.0:5000" by default instead of usage the default port 5000 by default (to be exact the usage of http://localhost:5000). The call of .UseConfiguration(config) are made "https://*:1234"0 .UseUrls. Thus the configuration loaded from hosting.json or the command line overwrite the default options. If one remove .SetBasePath(Directory.GetCurrentDirectory()) line then the hosting.json will be loaded from the same directory where the application dll will be compiled (for example bin\Debug\netcoreapp1.0).

One can use execution like

dotnet.exe run --server.urls=http://0.0.0.0:5000

to overwrite the default settings (from UseUrls) and the settings from "server.urls" property of hosting.json if it's exist.

In the same way one could overwrite the ULR settings by setting the environment variable

set ASPNETCORE_SERVER.URLS=http://localhost:12541/

then the default start of the application using dotnet.exe run will use http://localhost:12541/ for binding.

You can find here an example of the usage of HTTPS binding.

REMARK: The name of environment variable is changed from ASPNETCORE_SERVER.URLS to ASPNETCORE_URLS in later versions of ASP.NET(see here the documentation of ASP.NET Core 3.1).

In RC2 the commands section of project.json is no longer used. I haven't gotten Kestrel to pick up the hosting.json yet, but you can programatically set the port in the Main of the application where the new WebHostBuilder is created and configured. Just add .UseUrls() method like in the sample below

    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();
}

If you are trying to put an ASP.NET Core application inside a docker container (which was my use case for needing to listen to non-localhost addresses), note that this use case has already been put together for you by Microsoft. You can see the full glory at https://hub.docker.com/r/microsoft/aspnetcore/

At current (v1.0.1) the key magic for solving this problem is that the source Dockerfile contains a url environment variable setting, and the application does not attempt to override this. (Indeed, a containerized application should internally assert as little as possible about the environment where it will run.)

ENV ASPNETCORE_URLS http://+:80

Note the plus sign rather than asterisk there. I actually recommend visiting the above dockerhub link over reading my answer for as long as the link is good. Version 1.1 is just around the corner, and things may change again in the future.

When running the container, make sure to expose guest port 80, per the environment variable setting. For example:

docker run -d -p 8000:80 myapp
curl localhost:8000

If you use asp.net core 2.1+,modify config section in appsettings.json.

"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5002"
}
}
},

Set the environment variable ASPNETCORE_URLS to http://0.0.0.0:5000/.

If running from Visual Studio, you add the environment variable from the Debug tab of the project properties.

For AspNetCore 3.1+ just add the following line in the file appsettings.json:

"Urls": "http://*:80"

Another option is to modify launchSettings.json. Replace "applicationUrl": "http://localhost:5000", with "applicationUrl": "http://0.0.0.0:5000",. Like so:

{
"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"
}
}
}
}