239

From previous versions of the question, there is this: Browse website with ip address rather than localhost, which outlines pretty much what I've done so far...I've got the local IP working. Then I found ngrok, and apparently I don't need to connect via the IP.


What I am trying to do is expose my website running on localhost to the internet. I found a tool that will do this: ngrok.

Running the website in visual studio, the website starts up on localhost/port#. I run the command "ngrok http port#" in the command line. Everything seems to start up fine. I generate a couple of URLs, and the ngrok inspection url (localhost:4040) works.

The only problem is that when I go to the generated URLs, I get an HTTP error 400: bad request invalid hostname. This is a different error than when I run "ngrok http wrongport#", which is a host not found error...so I think something good is happening. I just can't tell what...

Is there a step I am missing in exposing my site to the internet via the tunneling service? If there is, I can't find it in the ngrok documentation.

Community
  • 1
  • 1
Chris
  • 21,703
  • 21
  • 59
  • 114
  • The whole process is summarized [here](https://stackoverflow.com/a/67450945/3188225). Maybe this could help. – umunBeing May 08 '21 at 19:11

6 Answers6

646

Troubleshot this issue with ngrok. In the words of inconshrevable, some applications get angry when they see a different host header than expected.

Running the following command should fix the problem:

ngrok http [port] -host-header="localhost:[port]"
Chris
  • 21,703
  • 21
  • 59
  • 114
  • 3
    Thanks. I previously followed the instructions from Devin Rader detailed here, but this now makes this way easier. https://www.twilio.com/blog/2014/03/configure-windows-for-local-webhook-testing-using-ngrok.html – sobelito Dec 06 '15 at 22:25
  • 1
    This solution also works if you are having trouble adding another binding to the applicationHost.config (vs2015/iisexpress). You don't need to add one just use this answer. Finally, if you paid for a custom domain to avoid changing the address every time just add -subdomain=mysubdomain to above answer. – trevorc Dec 18 '15 at 21:41
  • It worked. Just in case of using Social Authentication in asp.net 5 mvc 6, somehow, the return URL is becoming http://localhost/signin-facebook instead of using my subdomain. Any idea how to fix that? – skjoshi May 28 '16 at 07:17
  • @Sanju Not sure; it's been a year since I've used Ngrok – Chris Jun 09 '16 at 20:26
  • 4
    Can't upvote this enough! For all the complex and buggy instructions out there to hack your IISExpress bindings and network settings, this cuts through it all like a hot knife through butter. – Neil Laslett Mar 01 '18 at 16:54
  • 30
    When local is on https instead of http then this variation of above works: `ngrok http https://localhost:44362 -host-header="localhost:44362"` – Jsinh Jun 01 '19 at 09:33
  • 1
    wow, I wasted almost 2 hours figure this one out. my only problem was I was adding https in -host-header. like this: `ngrok http https://localhost:44392 -host-header="https://localhost:44392"` removing https:// from -host-header parameter solved my problem. Thanks – Pirate Jun 07 '19 at 15:24
62

Following command will fix the issue

ngrok http -host-header=localhost 8080
Sathish
  • 1,733
  • 11
  • 13
  • 9
    If using ASP.NET Core you should also comment out `app.UseHttpsRedirection()` in your Startup class to avoid a 307 Temporary Redirect. – Mark G Nov 14 '19 at 21:58
41

This didn't work for me. you could do the following:

For IIS Express

In VS 2015: Go to the .vs\config\applicationhost.config folder in your project

In VS 2013 and earlier: Go to %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config

Find the binding that says:

<binding protocol="http" bindingInformation="*:5219:localhost" />

For me it was a project running on port 5219

change it to

  <binding protocol="http" bindingInformation="*:5219:" />

IIS Express will now accept all incoming connections on that port.

Disadvantage: you need to run IIS Express as admin.

or you could rewrite the host header in Ngrok:

ngrok.exe http -host-header=rewrite localhost:5219
Stefanvds
  • 5,795
  • 4
  • 45
  • 69
  • This is the correct way to rewrite header in ngrok. None of the other answers worked. Already got IIS set to any hostname, but ngrok returned "hostname invalid" when using -host-header="localhost:8892" however this rewrite version worked immediately. – Tyeth Mar 11 '20 at 15:43
15

For https this works:

ngrok http https://localhost:<PORT> -host-header="localhost:<PORT>"

Joel Wiklund
  • 1,173
  • 2
  • 9
  • 18
1

The simplest thing for me was using iisexpress-proxy + ngrok.

First I install iisexpress-proxy globally with npm

npm install -g iisexpress-proxy

Then I proxy my localhost with it. Say for instance my site is running on 3003.

iisexpress-proxy 3003 to 12345 where 12345 is the new http port I want to proxy to.

Then I can run ngrok on it.

./ngrok.exe http 12345

It just works!

But I think it works only with http. Right now I don't use https to test, but even if it works, usually it's a lot of work as always.

Reuel Ribeiro
  • 1,129
  • 11
  • 21
-7

Steps.

  1. Run command on your console from ngrok.exe directory . ngrok http port i.e ngrok http 80 https://www.screencast.com/t/oyuEPlR6Z Set

  2. Ngrok url to your app .

It will create a tunnel to your application.

Thanks .