如何让 kestrel Web 服务器侦听非本地主机请求? [英] How do I get the kestrel web server to listen to non-localhost requests?

查看:38
本文介绍了如何让 kestrel Web 服务器侦听非本地主机请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've deployed my c#, asp.net 5, mvc 6 app to a windows 2008 server. I've fired up dnx web and it is listening to port 5000 and works fine when accessing from local computer.

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

How do I get it to listen to non-localhost requests?

附言这个问题不是这个的重复...它指的是当hosting.ini 实际上有一个.ini 格式时,asp.net pre RC1.现在,它是 JSON,我找不到任何关于它实际应该包含什么的文档.

P.S. This question is not a duplicate of this...it refers to asp.net pre RC1 when hosting.ini actually had an .ini format. Now, it's JSON and I can't find any documentation on what should actually be in it.

P.P.S.真正的解决方案是在链接问题的未被接受的答案中,并带有大量警告.步骤:

P.P.S. The real solution is in the non-accepted answer to the linked question, with a massive caveat. Steps:

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

附注对于支持这个问题的人.它已经过时了.非常过时!

它适用于 .NET Core 的早期版本.问题和答案当然不适用于当前版本的框架(例如 2.x、3.x)

P.S. For people upvoting this question. It's outdated. Very badly outdated!

It applied to the early versions of the .NET Core. The question and the answers certainly aren't applicable for the current versions of the framework (e.g. 2.x, 3.x)

推荐答案

Kestrel 服务器使用的默认配置文件是 hosting.json.该名称在不同的 Beta 版本中多次更改.如果您现在将 project.json 与以下 "command" 部分一起使用

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

文件 hosting.json 将被读取.文件

the file hosting.json will be read. The file

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

将服务器配置为在每个 IP4 地址上侦听 5000.配置

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

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

将通知在 IP4 和 IP6 地址上侦听 5000.

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

可以通过使用ASPNET_ENV 环境变量或使用--config myconfig1.json(或config=myconfig1.json).例如你可以使用

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

并创建具有特定配置的 hosting.Development.json 文件.或者,您可以将 project.json

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

我必须另外提醒,您可能需要允许额外收听和注册(以启动 dnx web).由于防火墙和侦听新 TCP/HTTP 端口的本地安全性,它是必需的.像下面这样的东西应该让每个人(IPv4 和 IPv6)本地注册和监听 5000 端口:

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.

更新:谢谢@BlaneBunderson.可以使用 * 而不是 IP 地址(如 http://*:5000)来侦听来自任何接口的任何 IP4 和 IP6 地址.一个应该小心,不要使用这些

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

因为它需要注册IP6地址::或IP4地址0.0.0.0两次.

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

对应公告

从技术上讲,任何不是localhost"或有效 IPv4 的主机名或IPv6 地址将导致 Kestrel 绑定到所有网络接口.

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

我认为未来可能会改变这种行为.因此,我建议仅使用 *:50000.0.0.0:5000::5000 表格来注册任何 IT 地址.

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.

更新 2: ASP.NET Core RC2 更改(请参阅 公告) 加载默认值的行为.必须在 Main 中进行更改才能从 hosting.json 和命令行参数加载设置.下面是一个用法示例

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

上面的代码使用了三个绑定:"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 的同一目录加载(例如 <代码>binDebug etcoreapp1.0).

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 after .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 binDebug etcoreapp1.0).

可以像这样使用执行

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

覆盖默认设置(来自 UseUrls)和来自 "server.urls" 属性的设置 hosting.json 如果它存在.

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

同样可以通过设置环境变量来覆盖 ULR 设置

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

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

然后应用程序的默认启动使用 dotnet.exe run 将使用 http://localhost:12541/ 进行绑定.

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

您可以在此处找到HTTPS 绑定的使用.

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

备注:在更高版本的 ASP.NET 中,环境变量的名称从 ASPNETCORE_SERVER.URLS 更改为 ASPNETCORE_URLS(请参阅此处 文档ASP.NET Core 3.1).

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).

这篇关于如何让 kestrel Web 服务器侦听非本地主机请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆