Search code examples
c#nancy

Unable to access host created by Nancy using external IP


I am creating a Nancy Module that will eventually be hosted inside of a Windows Service. To start the Nancy hosting, I am using Nancy.Hosting.Self. Below is the code to start Nancy host.

string strHostProtocol = Convert.ToString(ConfigurationManager.AppSettings["HostProtocol"]);
string strHostIP = Convert.ToString(ConfigurationManager.AppSettings["HostIP"]);
string strHostPort = Convert.ToString(ConfigurationManager.AppSettings["HostPort"]);

//Here strHostProtocol="https", strHostIP = "192.168.100.88" i.e. System IPv4, strHostPort = "9003"

var url = strHostProtocol + "://" + strHostIP + ":" + strHostPort;
//url ="https://192.168.100.88:9003"

this.host = new NancyHost(new Uri(url));
this.host.Start();

Now once the windows service starts, it will start the above host and I could see this in netstat -a command. When I browse this in browser using https://192.168.100.88:9003 I will get proper response.

The problem arises when the same is browsed using its external IP. Say this system has been assigned with external IP of 208.91.158.66 and when I try browsing this as https://208.91.158.66:9003 I will just get a browser default loading progress continuosly which does not stop and without any error thrown. I have also added the below command and reserved URL successfully.

netsh http add urlacl url=https://192.168.100.88:9003/ user=everyone

But even after this the host cannot be browsed using external IP assigned to that system. Is there any restricting Nancy is putting up? Firewalls are turned off, defenders are turned off. Anyone has any idea on this?

UPDATE

The duplicate linked question talks about LAN but here I am trying through external IP and I've tried answer mentioned over there and also specified the same in question


Solution

  • Alright. This issue was also posted to GitHub Nancy Repo and below is what @Khellang had to say.

    When you bind to https://192.168.100.88:9003, the TcpListener/HttpListener won't listen on other interfaces. You either have to bind to https://208.91.158.66:9003 or https://localhost:9003 and set RewriteLocalhost = true (default).

    Further he also said that

    If you also want to listen to requests coming to the external IP, yes. Or you could use a wildcard, like https://+:9003/, https://*:9003/ or https://localhost:9003/ (with RewriteLocalhost = true, this will result in https://+:9003/). You can read more about them in the link I posted.

    and thanks to @TimBourguignon as he suggested the same in his comments. Hope this helps someone in future.

    He has also suggested to read this link to know more about the Strong Wildcard and Weak Wildcard