How to get kestrel web server to listen for requests not related to localhost?

I deployed my C #, asp.net 5, mvc 6 application for a Windows 2008 server. I ran dnx web and it listens on port 5000 and works fine when accessed from the local computer.

How do I get it to listen for requests not related to localhost?

PS This question is not a duplicate of this ... it refers to asp.net pre RC1 when the .ini hosting actually had the .ini format. Now, JSON and I cannot find the documentation of what should actually be in it.

PPS The real solution lies in the unacceptable answer to the related question with great caution. Steps:

  • Change your project.json to a related answer.
  • Publish the project on your server.
  • On the server, go to the folder ... \ approot \ src \ YourProject and open the command window there.
  • Run dnx web - it will not work
  • Run dnu restore
  • Run 'dnu build`
  • Run 'dnx web` - the web server should start fine
+46
c # asp.net-core dnx
Dec 10 '15 at 22:18
source share
3 answers

The default configuration file used by the Kestrel server, hosting.json . The name has been changed several times in different beta versions. If you are now using project.json with the following "command" section

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

then when starting the server from the command line on

 dnx web 

the hosting.json file will be read. File

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

configure the server to listen on 5000 on each IP4 address. Configuration

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

will tell you to listen to 5000 on both IP4 and IP6.

Alternative configuration files can be specified using the ASPNET_ENV environment ASPNET_ENV or by using --config myconfig1.json (or config=myconfig1.json ). For example, you can use

 SET ASPNET_ENV=Development 

and create a hosting.Development.json file with a 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 using

 dnx webProd 

I need to be further reminded that you may need additional listening and registration (to run dnx web ). This is necessary due to the firewall and local security when listening on new TCP / HTTP ports. Something like below should do local registration and listening for 5000 ports for all (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 configure the above configuration to provide minimal rights.

UPDATED: Thanks @BlaneBunderson. You can use * instead of an IP address (for example, http://*:5000 ) to listen on any IP4 and IP6 addresses from any interface. You need to be careful not to use http://*:5000;http://::5000 , http://::5000;http://*:5000 , http://*:5000;http://0.0.0.0:5000 or http://*:5000;http://0.0.0.0:5000 , because for this you will need to double-register the IP6 :: address or IP4 address 0.0.0.0 .

Matches Ad

Technically, any hostname that is not โ€œlocalhostโ€ or a valid IPv4 or IPv6 address will result in Kestrel binding to all network interfaces.

I think that behavior may change in the future. Thus, I would recommend using only the forms *:5000 , 0.0.0.0:5000 and ::5000 to register any IT address.

UPDATED 2: Changes in ASP.NET Core RC2 (see announcement ) default load behavior. You need to make changes to Main to load the settings from hosting.json and command line options. Below is an example of use

 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 uses three bindings: "http://*:1000" , "https://*:1234" , "http://0.0.0.0:5000" by default instead of using the default 5000 ports by default (more precisely , use http://localhost:5000 ). The .UseConfiguration(config) call is made after .UseUrls . Thus, the configuration loaded from hosting.json or the command line overwrites the default settings. If you delete the line .SetBasePath(Directory.GetCurrentDirectory()) , then hosting.json will be loaded from the same directory where the application dll will be compiled (for example, bin\Debug\netcoreapp1.0 ).

You can use execution, for example

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

to overwrite the default settings (from UseUrls ) and the settings from the "server.urls" hosting.json property, if they exist.

In the same way, you can overwrite the ULR settings by setting the environment variable.

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

then by default running the application using dotnet.exe run will use http://localhost:12541/ to bind.

You can find an example of using HTTPS bindings here .

+74
Dec 11 '15 at 10:47
source share

In RC2, the project.json command section is no longer used. I have not yet received Kestrel to pick up .json hosting, but you can programmatically set the port in the Main application where the new WebHostBuilder is created and configured. Just add the .UseUrls () method, as in the example 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(); } 
+24
May 20 '16 at 14:52
source share

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

Currently (v1.0.1), the key magic to solving this problem is that the Dockerfile source file contains the url environment variable parameter, and the application does not try to override this. (Indeed, a container application should internally claim as little as possible about the environment in which it will run.)

 ENV ASPNETCORE_URLS http://+:80 

Pay attention to the plus sign, not the asterisk. I actually recommend visiting the above dockerhub link by reading my answer as long as the link is good. Version 1.1 is just around the corner, and things can change in the future.

When starting the container, make sure that you open guest port 80 in the environment variable setting. For example:

 docker run -d -p 8000:80 myapp curl localhost:8000 
+4
Oct 25 '16 at 13:45
source share



All Articles