120

Is it possible to open multiples ports in ngrok in same domain?

Something like:

Fowarding http://example.ngrok.com:50001 -> 127.0.0.1:50001

Fowarding http://example.ngrok.com:50002 -> 127.0.0.1:50002

I´m working in windows and it'll be useful for debuging with IIS Express

Nazmul
  • 575
  • 3
  • 18
fravelgue
  • 2,633
  • 2
  • 24
  • 23

8 Answers8

131

Yes, it is possible using multiple simultaneous tunnels, within the same hostname !

All you need to do, is to declare them on your configuration file, like this:

authtoken: 4nq9771bPxe8ctg7LKr_2ClH7Y15Zqe4bWLWF9p
tunnels:
  first-app:
    addr: 50001
    proto: http
    hostname: example.ngrok.com
    host_header: first-app.example.ngrok.com
  second-app:
    addr: 50002
    proto: http
    hostname: example.ngrok.com
    host_header: second-app.example.ngrok.com        

And run them with:

ngrok start --all

Look on the documentation for options, like hostname, subdomain, authtoken and host_header. Hope this help you !

P.S For Free plan remove custom host and header part like this it will be different domains FYI.

authtoken: 6yMXA63qefMZqCWSCHaaYq_5LufcciP1rG4LCZETjC6V
tunnels:
  first:
    addr: 3002
    proto: http    
  second:
    addr: 8080
    proto: http

NOTES:

Elânio
  • 51
  • 4
robe007
  • 3,523
  • 4
  • 33
  • 59
  • 11
    Does this work as OP actually needs? Doesn't this make a hostname for first-app and a different hostname for second-app? – user2152081 Feb 11 '17 at 23:32
  • @user2152081 Yes, of course it works as OP actually needs! You can use the same `hostname` within the differents `apps`. I have edited my question to show how. More information in the _docs link_ originally posted. – robe007 Feb 19 '18 at 16:51
  • As of ngrok 2.2.8, this does not work. It fails with the following error: – cap10morgan Feb 26 '18 at 19:03
  • 18
    As of ngrok 2.2.8, this doesn't work. It fails with the following error: `Tunnel session failed: The tunnel 'http://example.ngrok.com' is already bound to another tunnel session ERR_NGROK_334` ...for reserved hostnames. – cap10morgan Feb 26 '18 at 19:05
  • @cap10morgan It's a bit weird. I have edited the answer adding more options like `authtoken` and `host_header`. Please, read the [docs](https://ngrok.com/docs#config-options) first to understand how they works. – robe007 Feb 26 '18 at 19:56
  • @cap10morgan And please, don't forget to install your `auth_token` first. See [here](https://ngrok.com/docs#authtoken) – robe007 Feb 26 '18 at 20:05
  • 1
    it says i have to upgrade :( `Tunnel session failed: Only Pro & Business plans may bind custom hostnames. Failed to bind the custom hostname 'sajilni.example.ngrok.com' for the account 'shareef hiasat'. This account is on the 'Free' plan. Upgrade to a paid plan at: https://dashboard.ngrok.com/billing/plan ERR_NGROK_314` i had to remove custom host name – shareef Apr 04 '18 at 06:06
  • @shareef If you look on the _edition history_, what you added was my [original answer](https://stackoverflow.com/revisions/38645042/1) – robe007 Apr 04 '18 at 17:16
  • 1
    This still doesn't seem to work. You can't use multiple tunnels on the same hostname. – Gabriel Syme May 22 '18 at 17:46
  • 1
    This doesn't work for free plans. You have to upgrade to either a "pro" or "business" plan. – RavenHursT May 24 '18 at 17:01
  • 28
    After spending half a day chasing this & even signing up for a "pro" account Ive determined that this isn't something `ngrok` supports. All web traffic through the generated subdomains on ngrok.io TLD will _only_ listen on port `:80`. To listen on another, non-standard HTTP port, you have to use their TLS "reserved" domains. But those are randomly assigned a port when they are registered, and they won't listen on the same domain as the HTTP tunnel. In the end, this answer does _not_ give a valid solution the OP's question. down-voting 2 indicate 2 others not 2 waste their time chasing this – RavenHursT May 24 '18 at 19:23
  • 1
    With a paid plan, you just need to have two different `example.ngrok.com` hostnames but it works !!! – A. Attia Aug 03 '18 at 14:44
  • I agree with RavenHursT – PeopleMoutainPeopleSea Jan 31 '19 at 05:50
  • 1
    This does not work! The Ngrok side of the tunnel is limited to port 80 as RavenHursT suggests. – Code Junkie Feb 13 '19 at 09:35
  • Downvoting as this doesn't work anymore unfortunately – Alex Feb 15 '19 at 08:09
  • 1
    You are a saviour. This is working perfectly for the free plan as of June, 2020. – Maximiliano Guerra Jun 08 '20 at 14:20
  • 1
    @MaximilianoGuerra Hey even i am trying to work on this how u made it work any help would be very appreciated i am not able to get it done.I want to try two python code running using ngrok and listening to different ports in the same domain. – RAHUL VISHWAKARMA Jun 22 '20 at 08:43
  • 5
    @RAHULVISHWAKARMA I did exactly like the last code in the answer. In my case I needed 3 ports, but each one created 2 tunnels (http and https), going above the 4 tunnels limit of the free version. I changed the config to create only https tunnels and everything worked perfectly, still have one spot for other tunnel: https://gist.github.com/maximiliano/b69906e513e5b6860327d821bc0578af In case it wasn't clear, you have to edit the ngrok config file in your machine and run like: `ngrok start --all` – Maximiliano Guerra Jun 22 '20 at 15:07
  • 3
    @MaximilianoGuerra thanks it is working fine and perfect – RAHUL VISHWAKARMA Jul 23 '20 at 07:19
  • 2
    Doesn't work with free ngrok account because different URLs are assigned to `first` and `second`. – Chupo_cro Mar 31 '21 at 23:06
  • Welp. I have the basic plan, but the first example (with hostname and host_header) did not work for me. It says the tunnel (on my custom hostname) is already bound to another tunnel session. Ngrok doesn't appear to support it. HOWEVER, the free version in @MaximilianoGuerra gist with no hostname config DOES work for me, perfectly. Only downside is you get randomly assigned a domain for each one. – cheryllium Apr 04 '22 at 17:34
  • Suggest updating answer to clarify that ngrok doesn't support this feature officially - so we don't waste time paying for plans only to find we still can't do it. – cheryllium Apr 04 '22 at 17:35
  • Please edit the "free" section, as it is not correct. With a free plan it is not possible to have 2 ports in the same domain – eMarine Apr 27 '22 at 20:44
  • @robe007 this solution only seems to work with up to 3 tunnels (on the free ngrok version). Is there a way to add more than three? – andrea Apr 27 '23 at 15:02
36

What worked for me with ngrok w/ multiple ports

So I had the issue where I needed the same domain origin policy to work for different ports but I was halted in my tracks because ultimately ngrok does not support this. They support a custom subdomain or custom domain but not on different ports since all must come through port 80 or 443.

Instead of quitting, I had to hack things together using nginx locally like so:

http {
    server {
        listen       7777;
        server_name  localhost;

        location / {
            proxy_pass http://127.0.0.1:5000;
        }

        location /api {
            proxy_pass http://127.0.0.1:8000;
        }
    }
}

I was fortunate the api server prefixed all calls "api" so I could route the api calls to a specific port and still serve the other traffic on another web server and you may not be so lucky.

I then configured the public web server to route all api calls to the same ngrok address and let ngnix sort it out.

I hope this may help you think of a combination of solutions to get there as thinking only one way may get you stuck as I was.

King Friday
  • 25,132
  • 12
  • 90
  • 84
  • 4
    This should actually be the answer and not the one above. I paid ngrok then figured, they cannot do it and lead me to this thread. Now, I have to cancel my billing and ask for a refund. lol. – Louie Miranda Aug 07 '20 at 02:14
  • 1
    this should be the answer, thanks for your contribution!, I paid 60$/year to ngrok wanting to have multiple process for more than 1 app running in my server but then having issues doing this so, It does not makes sense to pay such amount of money I think, I would keep using the free plan, refund!! – Oswaldo Zapata Dec 18 '20 at 20:07
  • Different ports work well but the problem is when the same URL has to be used with different ports (e.g. ESP32 CAM CameraWebServer serves still pictures on port 80 but video stream is on port 81). – Chupo_cro Mar 31 '21 at 23:12
  • @OswaldoZapata One man's trash is another's treasure... Thanks for sharing! Saved quite some money, well, mine surely –  Jul 08 '21 at 19:13
19

Go To These location :

  • OS X: /Users/example/.ngrok2/ngrok.yml
  • Linux: /home/example/.ngrok2/ngrok.yml
  • Windows: C:\Users\example\.ngrok2\ngrok.yml

then open yml file in notepad and paste below code and save.

authtoken: your_token
tunnels:
  first:
    addr: 3002
    proto: http    
  second:
    addr: 8080
    proto: http

now go to your ngrok location and run ngrok start --all

Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38
spidy
  • 539
  • 1
  • 6
  • 7
5

Unfortunatly none of the following solutions worked for me but after multiple hours typing code with my nose i figured a way to solve this problem:

authtoken: your_private_token
tunnels:
  baseAPI:
    proto: http
    addr: https://localhost:44307/
    host_header: localhost:44307
  authAPI:
    proto: http
    addr: https://localhost:44305/
    host_header: localhost:44305

The diference is instead of using only the port on the addr field, i used the full link and added the host_header too.

4

This is how you can do using subdomain (Following @robe007 answer)

authtoken: your_auth_token
region: au
tunnels:
  frontend:
    proto: http
    addr: http://localhost:3000
    bind_tls: true
    subdomain: frontend-my-domain
    host_header: rewrite
  backend:
    proto: http
    addr: http://localhost:5001
    bind_tls: true
    subdomain: backend-my-domain
    host_header: rewrite

Then run ngrok start --all

Viraths
  • 840
  • 1
  • 13
  • 23
  • NOTE: `ERROR: Only paid plans may bind custom subdomains` (but one can remove the `subdomain`) – srghma Aug 19 '22 at 18:19
4

Using free plan

  1. Actually, at 2023, I'm just supporting the original answer.

  2. And, showing how to edit the ngrok config file (ngrok.yml).

In CMD do:

ngrok config edit

Something like this:

version: "2"
authtoken: your_token_here
tunnels:
  any1:
    addr: 8888
    proto: http
  any2:
    addr: 8080
    proto: http
  any3:
    addr: 50000
    proto: http

Finally, again in CMD, start ngrok:

ngrok start --all

Be happy!

marcio
  • 566
  • 7
  • 19
3

I used on ngrok process (on a reserved domain) pointing to port 80 locally.

ngrok http 80

Locally I have nginx running the following config. The only thing that matters to me are the 2 locations /admin and /api as those are what I was previously using multiple ngrok prcesses for. nginx allows you to use the same tunnel for separate locations.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    server_name _;

    location /admin {
        proxy_pass http://127.0.0.1:3005;
    }

    location /api {
        proxy_pass http://127.0.0.1:5001;
    }
}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
1

I'm using a free plan

If you want to run multiple domains on https, this worked for me

ngrok config edit
version: "2"
authtoken: --your-ngrok-auth-token--
tunnels:
  api1:
    addr: https://localhost:8888
    schemes: 
      - https
    proto: http
  api2:
    addr: https://localhost:8080
    schemes: 
      - https
    proto: http

After that run

ngrok start --all
Fer
  • 3,247
  • 1
  • 22
  • 33
Stefan Popovski
  • 486
  • 6
  • 22