我如何获得红隼Web服务器,听取非本地主机的请求? [英] How do I get the kestrel web server to listen to non-localhost requests?

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

问题描述

我已经部署了我的C#,asp.net 5,6 MVC应用到Windows 2008服务器。我已经解雇了 DNX网站并正在监听5000端口,并从本地计算机上访问时,工作正常。

我如何得到它来听非本地主机的请求?

P.S。这问题是不是这样重复...它是指asp.net pre RC1时hosting.ini实际上有一个.ini格式。现在,它是JSON和我找不到什么实际上应该是在它的任何文档。

P.P.S。实际的解决方法是在非公认回答以链接的问题,具有巨大的警告。操作步骤:


  1. 更改project.json每个链接的答案。

  2. 发布项目到你的服务器。

  3. 在服务器上,转到... \\为approot的\\ src \\ YourProject文件夹,打开命令窗口那里。

  4. 运行 DNX网站 - 它会失败

  5. 运行 DNU恢复

  6. 运行DNU build`

  7. 运行DNX web` - Web服务器现在应该启动精细


解决方案

由红隼服务器使用默认的配置文件是 hosting.json 。这个名字是在不同的测试版本多次更改。如果你现在 project.json 使用具有以下命令部分

命令:{
    网络:Microsoft.AspNet.Server.Kestrel
}

然后通过

启动命令行的服务器中

DNX网

文件 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 环境变量或的使用指定替代的配置文件 - 配置myconfig1.json (或配置= myconfig1.json )。例如,你可以使用

设置ASPNET_ENV =发展

和创建 hosting.Development.json 与特定的配置文件。或者您可以使用 project.json

命令:{
    网络:Microsoft.AspNet.Server.Kestrel
    webProd:Microsoft.AspNet.Server.Kestrel --config prod.json
}

和按使用启​​动服务器

DNX webProd

我要提醒另外,它可能需要您允许还听取并登记(启动 DNX网站)。它是必需的,因为防火墙和听新的TCP / HTTP端口的本地安全的。像下面的东西应该让地方登记和5000端口各位的聆听(IPv4和IPv6):

的netsh的http添加iplisten ip地址= 0.0.0.0:5000
netsh的HTTP添加iplisten ip地址= :: 5000
netsh的HTTP添加urlacl URL = HTTP:// +:5000 / USER = \\人人

要更安全,你可以调整上述配置授予最小的权限。

更新:感谢@BlaneBunderson。人们可以用*代替IP地址(如的http:// *:5000 )听上的任何 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 两次

对应公告


  

从技术上讲,任何主机不是本地主机或有效的IPv4或
  IPv6地址会导致红隼绑定到所有网络接口。


我认为该行为可能会在未来被改变。因此,我建议只使用 *:5000 0.0.0.0:5000 : :5000 形式对于任何IT地址的登记

更新3: ASP.NET核心RC2的变化(见公告)加载默认的行为。一个必须做出的更改加载从 hosting.json 设置和命令行参数。下面是使用的例子

公共静态无效的主要(字串[] args)
{
    无功配置=新ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile(hosting.json,可选:真)
        .AddEnvironmentVariables(preFIX:ASPNETCORE_)
        .AddCommandLine(参数)
        。建立();    VAR主机=新WebHostBuilder()
        .UseUrls(HTTP:// *:1000,https://开头*:1234,http://0.0.0.0:5000)
        .UseEnvironment(发展)
        .UseConfiguration(配置)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<&启动GT;()
        。建立();    host.Run();
}

以上code使用了三个绑定:的http:// *:1000https://开头*:1234 http://0.0.0.0:5000默认情况下,而不是使用默认的默认端口5000(准确的使用的的http://本地主机:5000 )。 由呼叫.UseConfiguration(配置)的之后 .UseUrls 。因此,配置与 hosting.json 加载或命令行覆盖默认选项。如果有删除 .SetBasePath(Directory.GetCurrentDirectory())线接 hosting.json 将来自同一个加载目录中的应用程序DLL会被编译(例如斌\\调试\\ netcoreapp1.0 )。

我们可以使用像执行

  dotnet.exe运行--server.urls = HTTP://0.0.0.0:5000

覆盖从server.urls的默认设置(从 UseUrls ),并设置的属性 hosting.json 如果它的存在。

在相同的方式人们可以通过设置环境变量覆盖ULR设置

 设置ASPNETCORE_SERVER.URLS =的http://本地主机:12541 /

然后用应用程序的默认启动dotnet.exe运行将使用的http://本地主机:12541 / 绑定。

这里

您可以找到 的HTTPS结合使用的例子。

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?

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. The real solution is in the non-accepted answer to the linked question, with a massive caveat. Steps:

  1. Change your project.json per the linked answer.
  2. Publish your project to your server.
  3. On the server, go to ...\approot\src\YourProject folder and open a command window there.
  4. Run dnx web - it will fail
  5. Run dnu restore
  6. Run 'dnu build`
  7. Run 'dnx web` - the web server should now start fine

解决方案

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

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

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