295

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
  • 28,822
  • 27
  • 83
  • 158
  • The whole process is summarized [here](https://stackoverflow.com/a/67450945/3188225). Maybe this could help. – umunBeing May 08 '21 at 19:11

12 Answers12

764

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]"

Depending on the version, you may also want to try:

ngrok http [port] --host-header="localhost:[port]"
pdoherty926
  • 9,895
  • 4
  • 37
  • 68
Chris
  • 28,822
  • 27
  • 83
  • 158
  • 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
  • 46
    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
  • 3
    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
  • This used to work for me, but appears you must have Pro or Enterprise version to get host header rewrite capabilities, at least that is the error that was returned to me today – lucky.expert Apr 18 '22 at 19:35
  • 3
    thanks, it still works in 2022. just as --host-header=whatever – LoLo Apr 26 '22 at 04:35
  • @LoLo Thanks. And yeah: I'd hold off on spending money pre-working prototype and continue to problem solve if you can't get it working. There might be something else wrong. – Chris Apr 26 '22 at 15:02
  • I set it as the subdomain and the terminal looks like it's redirecting fine but when I go to the ngrok link it takes me to the default welcome rails page instead of to my subdomain. Any ideas? – Walking Dec 09 '22 at 16:59
  • On my version of ngrok (3.3.0), the command is --host-header="localhost with two hyphens – rbrayb May 15 '23 at 22:41
70

Following command will fix the issue

ngrok http -host-header=localhost 8080
Sathish
  • 2,029
  • 15
  • 13
43

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,868
  • 5
  • 48
  • 72
  • 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
30

For https this works:

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

Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24
8

UPDATED COMMAND FOR LATEST VERSION

Tested with: (Windows) (ngrok v3.0.5)

Use -- instead of -

ngrok http --host-header=localhost 8080

Johar Zaman
  • 1,955
  • 17
  • 27
2

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,419
  • 14
  • 23
1

For https this works:

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

  • i am triyng to figure how to make it work with the tunnels configuration file ngrok.yml. When i get it, i'll edit this post :) – Pedro Dionisio Jun 14 '22 at 15:05
1

First open ngrok configuration YAML file, run from terminal:

ngrok config edit

Example of yaml for localhost setup (client & server):

version: "2"
authtoken: {YOUR_AUTH_TOKEN_FROM_NGROK_WEBSITE}
tunnels:
 client:
  addr: 3000
  proto: http
  host_header: localhost
 server:
  addr: 4000
  proto: http
  host_header: localhost

Save the config file based on your client and server ports and run the following command:

ngrok start --all

This will make ngrok open a tunnel for all the configurations declared in the yaml file

Adi Oz
  • 257
  • 1
  • 4
  • 8
0

Try with different locations from the Global infrastructure > Locations

ngrok http -region eu 8080

You can make a request and view any traffic passing through your tunnel using the ngrok traffic inspector at http://localhost:4040.

OR in command line

ngrok http -region eu 8080 --log=stdout                                                                                                                                                 

If one region fails then try with another.

ngrok runs tunnel servers in datacenters around the world. The location of the datacenter within a given region may change without notice (e.g. the European servers may move from Frankfurt to London).

  • us - United States (Ohio)
  • eu - Europe (Frankfurt)
  • ap - Asia/Pacific (Singapore)
  • au - Australia (Sydney)
  • sa - South America (Sao Paulo)
  • jp - Japan (Tokyo)
  • in - India (Mumbai)
itsazzad
  • 6,868
  • 7
  • 69
  • 89
0

Had IIS Express .net web API, had installed NGROK in docker (windows as a host) Had "Bad Request" error, the next command worked for me:

docker run -it -e NGROK_AUTHTOKEN=<token> ngrok/ngrok --host-header=localhost:21852  http host.docker.internal:21852

As I understood later, --host-header needed because IIS Express refuses all requests from outside (must be "localhost:port "), host.docker.internal I've used instead of localhost, because NGROK was running inside docker, while IIS Express was running on a windows host.

Viktor Fursov
  • 123
  • 1
  • 8
-1

I had the same issue and used the following solution:

  1. Make sure your application binding in your IIS is set to All Unassigned IP address

  2. Run ngrok HTTP 127.0.0.1:173 --region=eu --hostname=yourcustomdomain.eu.ngrok.io

That's it. Works perfectly. This solution is also for paid pro accounts

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • 1
    Hello, please read [Why is it considered inappropriate and unprofessional to type in all capital letters?](https://www.quora.com/Why-is-it-considered-inappropriate-and-unprofessional-to-type-in-all-capital-letters). Thank you. – Eric Aya Oct 12 '22 at 13:05
-8

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 .