我如何获得红隼Web服务器,听取非本地主机的请求? [英] How do I get the kestrel web server to listen to non-localhost requests?
问题描述
我已经部署了我的C#,asp.net 5,6 MVC应用到Windows 2008服务器。我已经解雇了 DNX网站
并正在监听5000端口,并从本地计算机上访问时,工作正常。
我如何得到它来听非本地主机的请求?
P.S。这问题是不是这样重复...它是指asp.net pre RC1时hosting.ini实际上有一个.ini格式。现在,它是JSON和我找不到什么实际上应该是在它的任何文档。
P.P.S。实际的解决方法是在非公认回答以链接的问题,具有巨大的警告。操作步骤:
- 更改project.json每个链接的答案。
- 发布项目到你的服务器。
- 在服务器上,转到... \\为approot的\\ src \\ YourProject文件夹,打开命令窗口那里。
- 运行
DNX网站
- 它会失败 - 运行
DNU恢复
- 运行DNU build`
- 运行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:// *:1000
,https://开头*: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 /
绑定。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:
- Change your project.json per the linked answer.
- Publish your project to your server.
- On the server, go to ...\approot\src\YourProject folder and open a command window there.
- Run
dnx web
- it will fail- Run
dnu restore
- Run 'dnu build`
- 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 nowproject.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
(orconfig=myconfig1.json
). For example you can useSET ASPNET_ENV=Development
and to create
hosting.Development.json
file with specific configuration. Alternatively you can useproject.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 usehttp://*:5000;http://::5000
,http://::5000;http://*:5000
,http://*:5000;http://0.0.0.0:5000
orhttp://*:5000;http://0.0.0.0:5000
because it will require to register IP6 address::
or IP4 address0.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 fromhosting.json
and the command line parameters. Below is an example of the usagepublic 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 ofhttp://localhost:5000
). The call of.UseConfiguration(config)
are made after.UseUrls
. Thus the configuration loaded fromhosting.json
or the command line overwrite the default options. If one remove.SetBasePath(Directory.GetCurrentDirectory())
line then thehosting.json
will be loaded from the same directory where the application dll will be compiled (for examplebin\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 ofhosting.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 usehttp://localhost:12541/
for binding.You can find here an example of the usage of HTTPS binding.
这篇关于我如何获得红隼Web服务器,听取非本地主机的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!