program story

로컬 호스트가 아닌 요청을 수신하도록 kestrel 웹 서버를 얻으려면 어떻게해야합니까?

inputbox 2020. 11. 1. 17:41
반응형

로컬 호스트가 아닌 요청을 수신하도록 kestrel 웹 서버를 얻으려면 어떻게해야합니까?


내 C #, asp.net 5, mvc 6 앱을 Windows 2008 서버에 배포했습니다. 나는 발사 dnx web했고 그것은 포트 5000을 듣고 있으며 로컬 컴퓨터에서 액세스 할 때 잘 작동합니다.

비 localhost 요청을 수신하려면 어떻게해야합니까?

추신이 질문이것 의 중복이 아닙니다 ... host.ini가 실제로 .ini 형식을 가지고있을 때 asp.net pre RC1을 참조합니다. 이제 JSON이고 실제로 무엇이 있어야하는지에 대한 문서를 찾을 수 없습니다.

PPS 실제 해결책은 큰 경고와 함께 연결된 질문에 대한 허용되지 않는 답변 에 있습니다. 단계 :

  1. 연결된 답변에 따라 project.json을 변경하십시오.
  2. 프로젝트를 서버에 게시합니다.
  3. 서버에서 ... \ approot \ src \ YourProject 폴더로 이동하여 명령 창을 엽니 다.
  4. 실행 dnx web-실패합니다
  5. 운영 dnu restore
  6. 'dnu build'실행
  7. 'dnx web'실행-이제 웹 서버가 정상적으로 시작됩니다.

Kestrel 서버에서 사용하는 기본 구성 파일은 hosting.json. 이름은 다른 베타 버전에서 여러 번 변경되었습니다. project.json다음 "command"섹션에서 지금 사용하는 경우

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

그런 다음 명령 줄에서 서버를 시작하는 동안

dnx web

파일 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환경 변수 또는 --config myconfig1.json(또는 config=myconfig1.json) 사용으로 대체 구성 파일을 지정할 수 있습니다 . 예를 들어 다음을 사용할 수 있습니다.

SET ASPNET_ENV=Development

hosting.Development.json특정 구성으로 파일 을 생성 합니다. 또는 다음 project.json과 함께 사용할 수 있습니다 .

"commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
    "webProd": "Microsoft.AspNet.Server.Kestrel --config prod.json"
}

사용법에 따라 서버를 시작하십시오.

dnx webProd

추가로 청취 및 등록 (시작하려면 dnx web) 을 허용해야 할 수 있음을 추가로 상기시켜야합니다 . 방화벽과 새로운 TCP / HTTP 포트를 수신하는 로컬 보안 때문에 필요합니다. 다음과 같이 모든 사람 (IPv4 및 IPv6)에 대해 5000 포트를 로컬로 등록하고 수신해야합니다.

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

보안을 강화하기 위해 위 구성을 조정하여 최소한의 권한을 부여 할 수 있습니다.

업데이트 : @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:5000IP6 주소 ::또는 IP4 주소를 0.0.0.0 두 번 등록해야하기 때문 입니다.

발표에 해당

기술적으로 "localhost"가 아니거나 유효한 IPv4 또는 IPv6 주소가 아닌 호스트 이름은 Kestrel이 모든 네트워크 인터페이스에 바인딩되도록합니다.

앞으로 행동이 바뀔 수 있다고 생각합니다. 따라서 모든 IT 주소 등록에는 *:5000, 0.0.0.0:5000::5000양식 만 사용하는 것이 좋습니다 .

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.


In RC2 the commands section of project.json is no longer used. I haven't gotten Kestrel to pick up the hosting.json yet, but you can programatically set the port in the Main of the application where the new WebHostBuilder is created and configured. Just add .UseUrls() method like in the sample 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();
    }

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

At current (v1.0.1) the key magic for solving this problem is that the source Dockerfile contains a url environment variable setting, and the application does not attempt to override this. (Indeed, a containerized application should internally assert as little as possible about the environment where it will run.)

ENV ASPNETCORE_URLS http://+:80

Note the plus sign rather than asterisk there. I actually recommend visiting the above dockerhub link over reading my answer for as long as the link is good. Version 1.1 is just around the corner, and things may change again in the future.

When running the container, make sure to expose guest port 80, per the environment variable setting. For example:

docker run -d -p 8000:80 myapp
curl localhost:8000

If you use asp.net core 2.1+,modify config section in appsettings.json.

 "Kestrel": {
"EndPoints": {
  "Http": {
    "Url": "http://0.0.0.0:5002"
  }
}

},

참고URL : https://stackoverflow.com/questions/34212765/how-do-i-get-the-kestrel-web-server-to-listen-to-non-localhost-requests

반응형