NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.15k stars 1.47k forks source link

Nancy doesn't listen to all interfaces on Windows Server 2019 #2974

Open Tommixoft opened 5 years ago

Tommixoft commented 5 years ago

Hello everyone. I know similar problems already was on internet and issues, but none of the solutions works. The problem is application works ok on Windows 7, Windows 10. I can access program port using local ip address, but DOESN'T on Windows Server 2019. I tried LITERALLY EVERYTHING. Problem is in NANCY, nowhere else. If i use my own written app that listens to the port - that app works fine in Windows Server 2019. If i look at Netstat i see that apps that works fine listens on 0.0.0.0:PORT, while Nancy app entry shows as 127.0.0.1:PORT and [::1]:PORT

my code:

                var hostConfig = new HostConfiguration
                {                    
                    RewriteLocalhost = true,
                    UrlReservations = new UrlReservations
                    {
                        CreateAutomatically = true
                    },
                };

                string url = $"http://localhost:{Session.Config.BindToPort}";
                host = new NancyHost(hostConfig, new Uri(url));
                host.Start();

Please dont tel me that code is fault or some firewall - ITs NOT. There is clearly problem with how nancy or .NET reserves url or something and Windows Server 2019 maybe acts differently that's why on other windows works fine.

Additional info:

Net 4.0 & 4.5.2 Nancy version: both 1.45 & 2.0 Os Windows Server 2019 Firewall disabled Url rules added manually, and automatically App have UrlReservation in code App was run as administrator and as Windows service

Tommixoft commented 5 years ago

The problem 100% because of it listens on 127.0.0.1 instead of 0.0.0.0 But how to achieve it to listen 0.0.0.0 on Win Server 2019 ? Microsoft changes something i guess :( If i run the same app on other OS netstat then shows listening on 0.0.0.0 instead of 127.0.0.1 and then it works,

khellang commented 5 years ago

The only thing Nancy does is rewrite http://localhost:<port> to http://+:<port> before adding a URLACL. If you want to prevent Nancy from doing anything "magic", you can turn off both by setting RewriteLocalhost to false and UrlReservations.CreateAutomatically to false. That way, you can add your own URLCLs so it works just like your other app.

Tommixoft commented 5 years ago

Thanks @khellang i will try, but i already tried adding rules manually...didn't helped. Anyway will try completely disable all the 'magic' of Nancy and try then. Will inform about results.

Tommixoft commented 5 years ago

Update. Well didnt work. To be fair UrlReservations not even needed if app uses TcpListener. So problem is not windows but Nancy implementation. I mean problem is .NET, but if you use TcpListener - there is no problems. Looks like HTTP is treated differently and that's the problem. At least in Windows Server 2019. Many people will come here with the same problem in the future. Looks like server dont want to allow communications to the registered http ports to be done externally at least those that is using .NET HttpListener class.

khellang commented 5 years ago

So problem is not windows but Nancy implementation

Nancy's self-host is just a super thin wrapper around HttpListener, with a tiny bit of "magic" to help people set up the correct URLACLs. If you disable that "magic" and do it all manually (and correctly), there's nothing that can be done in Nancy. It's either an issue with your setup or with Windows itself.

khellang commented 5 years ago

BTW, you haven't mentioned what the actual symptoms are, i.e. what errors you are getting. Only what you believe to be the cause. We've probably had a dozen "issue" reports about self-hosting not working over the years and in 90% of the cases, it's an issue with wrong URLACL setup. Wrong URL, wrong user, etc.

cocowalla commented 5 years ago

@Tommixoft Http.sys and the whole system of URL reservations is very fickle IME.

Might I suggest the following:

string url = $"http://127.0.0.1:{Session.Config.BindToPort}";

I seem to recall that worked for me in the past. Alternatively, you might try this:

`string url = $"http://*:{Session.Config.BindToPort}";

If that also doesn't work, it would be good to include some information about what actually happens when you try to connect - does it time out, or is the connection rejected, or something else?

Please dont tel me that code is fault or some firewall - ITs NOT. There is clearly problem with how nancy or .NET reserves url or something and Windows Server 2019 maybe acts differently that's why on other windows works fine.

Just some friendly advice - if you're asking OSS maintainers, contributers etc for help, this kind of language is really not a good way to encourage people to help you.

Tommixoft commented 5 years ago

@cocowalla i tried that too. Doesnt help. Thanks anyway.

The problem is that i can not access webservice created with nancy using local ip, only by using localhost or 127.0.0.1 Browser shows ERR_CONNECTION_REFUSED - error that is visible when you try to access port that nobody is listening.

Im not native english speaker so i do not know nice words. and my attitude good, but people maybe too soft sometimes. I just wanted to inform people that i dont want to read 10 post about how i did something wrong. When i clearly stated - in other windows everything works fine :) So problem is OS related and i wanted to find some other people who uses Nancy with Server 2019. That will be popular OS in year or two. I guess problem as i said is in HttpListener class and how os works with it. Looks like at least server 2019 doesn't want to allow that services would be accessible from outside, maybe there is some admin setting. For now i gonna try using other os.

But component creator, by the way thanks for this amazing plugin, may be interested in defeating this issue.