44

Quite new to using any sort of Web App stuff, and I've been trying to slowly build a Facebook Messenger Bot. When I try to use ngrok I can't visit the address I'm given, i.e:

ngrok http 5000

is what I'm putting in the command line, and it's returning this:

ngrok by @inconshreveable

Session Status                online
Version                       2.1.18
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://ea986ca5.ngrok.io -> localhost:5000
Forwarding                    https://ea986ca5.ngrok.io -> localhost:5000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

But when I take the address 'https://ea986ca5.ngrok.io' as is required by the Facebook developer's page, it says:

The connection to http://ea986ca5.ngrok.io was successfully tunneled to your
ngrok client, but the client failed to establish a connection to the local  
address localhost:5000.


Make sure that a web service is running on localhost:5000 and that it is a 
valid address.


The error encountered was: dial tcp [::1]:5000: connectex: No connection 
could be made because the target machine actively refused it.

Is it a problem with my local port? Thanks!

Jordan Cagney
  • 527
  • 1
  • 4
  • 8

17 Answers17

35

This worked for me

ngrok.exe http -host-header=rewrite localhost:

e.g ngrok.exe http -host-header=rewrite localhost:5219

Im using visual studio 2017 dont know if it effects anthing.

  • 27
    I also had to explicitly direct to https locally as well. I.e. `ngrok http https://localhost:8080 -host-header="localhost:8080"` – farlee2121 Mar 08 '19 at 14:54
26

Just as @njzk2 should have said, if you don't have a web server running so it cannot work. I would like to make it clearer to you, if you are still confused.

What ngrok does, is to make your local server (running on localhost) to be available to the outside world (rest of the internet). On its own, it is not a web server. So for your bot development you need to have a web server running on a defined port (which in your case is 5000). Then you can point ngrok to this port so that it will redirect requests sent to your public address to the program running on that port. The web server will then accept and handle requests from Facebook

Ken4scholars
  • 4,794
  • 2
  • 13
  • 28
21

Try to explicitly set the localhost IP:

ngrok http 127.0.0.1:5000 instead of ngrok http 5000

Good luck!

Sasha
  • 279
  • 2
  • 7
17

I found I had to remove the quotes around the -host-header section to get this to work with the latest ngrok version (2.3.35):

ngrok http https://localhost:5001 -host-header=localhost:5001

(Currently not enough rep to add comments on any of the answers above)

8a9
  • 305
  • 2
  • 8
9

Try like below:

ngrok http 127.0.0.1:8080 -host-header="127.0.0.1:8080"

licaomeng
  • 723
  • 1
  • 8
  • 19
  • 5
    Your answer is close but still did not work for me. Just little modification in your suggestion worked `ngrok http https://localhost:44398 -host-header="localhost:44398"` – Rahul Bagal Jan 31 '20 at 06:02
9

Step 1 - Register to ngrok and download .exe file in dashboard page

Step 2 - Open terminal and copy & paste from the dashboard the line without ./

Step 3 - run your localhost

Step 4 - If step 2 was successfully done, paste in the same terminal (step 2) the line with your localhost port

ngrok http https://localhost:44386 -host-header="localhost:44386"

Step 5 - Copy the URL generated in the terminal and waalla.. you can ping to it.


In case of <<<<Ngrok errors '502 bad gateway'>>>> change https into http so the Step 4 will be ngrok http http://localhost:44386 -host-header="localhost:44386"

Oltion Driza
  • 261
  • 2
  • 7
3

It seems that this issue has now been resolved in the latest version of ngrok: https://github.com/inconshreveable/ngrok/issues/448

Basically, what you do is to specify that you are using https like this:

ngrok http https://localhost:54321 -host-header="localhost:54321"

At least, this resolved the issue for me. Replace 54321 with your actual port number.

Eyvind
  • 4,993
  • 5
  • 38
  • 57
2

If you are trying to use ngrok to point to an https localhost url, set up a proxy.

see this github issue comment:

https://github.com/inconshreveable/ngrok/issues/448#issuecomment-414214242

Eric Aya
  • 68,765
  • 33
  • 165
  • 232
Davious
  • 1,661
  • 1
  • 13
  • 11
2

In my case, it was the project in Visual Studio 2017 .Net Core 2.1 was created to use https with a self-signed certificate. If you don't need your localhost to be https, then what fixed it for me was creating a new web project and unchecking https. When you run ngrok (ngrok http port-number-from-IISExpress) it provides you with an https url you can use for development.

Cliff Coulter
  • 337
  • 5
  • 20
1

For me, switching the protocol from http to tls worked since I am forwarding only a secure connection. I didn't need to rewrite the header.

Just for context, I am forwarding a connection to a running docker container on Ubuntu 16.

PS: You still access the address using https in the browser, not tls.

AndyFaizan
  • 1,565
  • 17
  • 28
0

This error can occur if you have an HTTP rule to redirect HTTP to HTTPS.

You can disable this for your developer machine or add a custom rule based on the X-Original-Host header:

I'm using the IIS rewrite plug-in and this is how I fixed it

 <rule name="Redirect to https" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" negate="false" />
            <conditions logicalGrouping="MatchAll">
              <add input="{HTTPS}" pattern="off" />

              <add input="{HTTP_X_Original_Host}" pattern="yourngrokname.ngrok.io" negate="true" />             

            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
          </rule>
Simon_Weaver
  • 120,240
  • 73
  • 577
  • 618
0

I had to use both (1) the answer from @user6483104 and (2) start my ngrok tunnel using the unsecured URL defined in my project (vs the SSL URL ie. https).

See my answer here: How to configure Visual Studio 2017 to expose a non-encrypted port in a ASP.Net MVC https site

Note: If I'm wrong about there being a default unsecured URL, this answer (How To Disable Https in Visual Studio 2017 Web Proj ASP.NET Core 2.0) claims to have a solution for disabling the secured URL. I didn't try it because there was already an unsecured URL defined in my existing project (as I suspect there is with yours as well)

enter image description here

Bret Royster
  • 441
  • 1
  • 4
  • 14
0

502 gateway error occurs because ngrok not able to receive any reponse from Localhost.

The things need to be done is :

  1. Start a webserver in a Port
  2. Trigger ngrok command in that Port.

What I have done is

  • Started a tomcat Web Server in 8080.
  • Then Triggered ngrok http 8080.

It works really cool ...
Try this .....
Hope it works for You .

Angel F Syrus
  • 1,607
  • 7
  • 18
  • 36
0

If you just with to use ngrok to intercept incoming data, and you do not have a local webserver for whatever reason, then you can use ncat.

nc -l 5000

This will create a process that listens on port 80.

So when used in combination with

ngrok http 5000

You will no longer get the '502 bad gateway' error.

Jeroen Ritmeijer
  • 2,783
  • 3
  • 22
  • 31
0

I had to put my server ip address and works perfect!

./ngrok http [ip server]:5000

0

i had the same issue,

because, i forgot to run the localhost:3000 port, which was supposed to be running some node server.

after i run node server, then i run, ngrok http 3000, so that it will cloud that node server in ngrok for globally access in internet.

0

MAKE SURE SERVER LISTENS ON THE SAME PORT WHERE NGROK IS TUNNELED

In my case I was running ngrok using 'ngrok HTTP 3002', while my express server was running at port 5000, hence throwing BAD GATEWAY error.

I restarted ngrok on port 5000 and it worked like a charm.