Kestrel Introduction
in ASP. NET core, our Web application is actually running on the Kestrel service, a Web server that runs ASP. NET core based on LIBUV Open source cross-platform.
During the development phase, we can use the Kestrel server directly for testing, or we can use iisexpress. In the use of iisexpress actually also need to start a Kestrel server, through iisexpress reverse proxy request to Kestrel, many times I prefer to use kestrel, because I can see the log in real time.
Configure ports
In socket development, the server will be bound to an IP port for listening, waiting for the client's connection, and then exchanging data, Kestrel also need to listen to a port, the client will request this port and establish a connection for data exchange. The configuration URL or configuration port we're talking about is essentially a port-based listener.
Configuration rules
We know that binding IP and ports are implemented in kestrel by binding the URLs parameter, and. Net core allows us to implement binding URLs in a number of ways, and we first understand the rules for binding:
[Http|https]://[ip|localhost|hostname]:p ORT
- localhost or 127.0.0.1 represents the native IP, allowing only native access
- LAN IP, allowing client access within the LAN
- Port 0 for randomly bound available ports
- ' * ' on behalf of 0.0.0.0, allow local, LAN, public network access
' * ' is not a special character, any character that cannot be recognized as an IP will be bound to 0.0.0.0,so, and you see that the HOSTNAME:IP is not actually bound to hostname,kestrel hostname, so it is not allowed to be like IIS, Multiple application are bound to the same port on the same IP through hostname, so you need to do this through a reverse proxy server
With the above binding string, the Kestrel resolves to the appropriate IP and port, and then binds the listener.
How to configure
. Net core provides a variety of configuration methods for Kestrel ports, which we can configure through encoding, configuration files, command line parameters, and then we look at various configurations.
Either way, we have to do it before the Kestrel starts, and in general we do it in Program.cs.
Encoding method
There are 2 ways of encoding:
1. Through Usekestrel (Action ):
var host = WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseKestrel(o => { o.Listen(IPAddress.Loopback, 5001); }) .Build();host.Run();
O.listen (Ipaddress.loopback, 5004) is the binding, where the first parameter is a IPAddress type. This method is not very convenient, reading is not good, we recommend the use of the second
2, through the Useurls way:
var host = WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseUrls("http://localhost:5002;http://localhost:5003") .Build();host.Run();
This is relatively simple and not prone to error, but not very flexible.
Through the configuration file
We can configure the kestrel with a JSON file, including our URLs.
1, first we need to create a JSON file, here Host.json for example:
{ "urls": "http://*:5004;"}
2. We need to tell Kestrel to read the config file when build host, the code is as follows:
public static void Main(string[] args){ var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("host.json", optional: true) .Build(); var host = WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseConfiguration(config) .Build(); host.Run();}
This is more convenient than coding, but if the web is running inside a container, it's a bit of a hassle to modify it, so let's look at the command-line format.
Command line mode
We know that. NET core we can use the dotnet command to run. NET core applications, which makes our web no longer dependent on IIS for cross-platform implementations.
Let us first understand the following commands:
> dotnet run [options] [[--] arguments]
The dotnet Run command will compile our project and run it directly at development time, and if it is a compiled project, use:
> dotnet yourproject.dll [[--] arguments]
If we need to configure URLs, just use the parameters --urls="http://*:5005"
, for example:
> dotnet run --urls="http://*:5005"
If you do this at this time, you will find that your project is not listening on port 5005 because you have not configured the kestrel to read the command line arguments, we need to configure the following when the build host:
public static void Main(string[] args){ var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddCommandLine(args) //添加对命令参数的支持 .Build(); var host = WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); host.Run();}
OK, now run the dotnet run
command again!
Questions about URL Configuration
Does the Q:url configuration support multi-domain names?
A: As stated above, Kestrel is not supported for hostname resolution, and your configuration will be bound to 0.0.0.0
Does the Q:url configuration support multiple IPs?
A: Support, but must be native IP, otherwise run error
Q: Can multiple Kestrel monitor a port?
A: Not
Q: Can I configure URLs in a variety of ways?
A: Yes, but there is only one, the final configuration, no priority.
Does Q:kestrel support HTTPS?
A: Support
Q: Why is servers.urls used in other tutorials?
A: I looked at, maybe the extension class is different, now it has changed to the URLs, and no need to refer to other class library.
Generation More ...
Written in the last
Recently looking at microservices and ASP. NET core, I also want to share some small knowledge with you.
Finally recommended my. Net Core QQ Learning Group: 376248054
(Customs Password: cnblogs), the recent group is not very active, we come in to speak more than speaking ha ~
Do kown ASP. NET Core--Configure Kestrel Port