share
Stack OverflowNgrok configure multiple port in same domain
[+149] [10] fravelgue
[2014-08-27 08:28:27]
[ iis-express ngrok ]
[ https://stackoverflow.com/questions/25522360/ngrok-configure-multiple-port-in-same-domain ]

Is it possible to open multiples ports in ngrok [1] 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

I need the port number in the ngrok url for testing purposes. Having multiple ngrok clients running will not help for that use case. - Per Quested Aronsson
[+165] [2016-07-28 19:20:55] robe007 [ACCEPTED]

Yes, it is possible using multiple simultaneous tunnels [1], 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:

Update: Nov 2024

for version 3, you need to do the following:

version: "3"
agent:
    authtoken: 2pNn6oWWEWEsdasc7xveBp2Q1TaPpl_3Jut8L2p4wCsdfEEF9BEcUk
      
tunnels:
  score_api:
    proto: http
    addr: 8000
  weather_api:
    proto: http
    addr: 9801
[1] https://ngrok.com/docs#multiple-tunnels

(14) Does this work as OP actually needs? Doesn't this make a hostname for first-app and a different hostname for second-app? - user2152081
@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
As of ngrok 2.2.8, this does not work. It fails with the following error: - cap10morgan
(20) 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
@cap10morgan It's a bit weird. I have edited the answer adding more options like authtoken and host_header. Please, read the docs first to understand how they works. - robe007
@cap10morgan And please, don't forget to install your auth_token first. See here - robe007
(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
@shareef If you look on the edition history, what you added was my original answer - robe007
(1) This still doesn't seem to work. You can't use multiple tunnels on the same hostname. - Gabriel Syme
(1) This doesn't work for free plans. You have to upgrade to either a "pro" or "business" plan. - RavenHursT
(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
(1) With a paid plan, you just need to have two different example.ngrok.com hostnames but it works !!! - A. Attia
I agree with RavenHursT - PeopleMoutainPeopleSea
(1) This does not work! The Ngrok side of the tunnel is limited to port 80 as RavenHursT suggests. - Code Junkie
Downvoting as this doesn't work anymore unfortunately - Alex
(1) You are a saviour. This is working perfectly for the free plan as of June, 2020. - Maximiliano Guerra
(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
(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: 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
(3) @MaximilianoGuerra thanks it is working fine and perfect - RAHUL VISHWAKARMA
(2) Doesn't work with free ngrok account because different URLs are assigned to first and second. - Chupo_cro
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
(1) 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
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
@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
1
[+45] [2019-02-13 20:48:36] King Friday

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.


(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
(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
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
@OswaldoZapata One man's trash is another's treasure... Thanks for sharing! Saved quite some money, well, mine surely - user8104581
2
[+27] [2021-07-09 14:10:14] spidy

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


(1) file location on my linux installation was /home/example/.config/ngrok/ngrok.yml - zwessels
now they store it this way: /home/<username>/.config/ngrok/ngrok.yml first line you will get the ver second your auth already their (if you registered before) then just paste as the answer. - ShifraSec
(7) You can find your ngrok.yml using ngrok config check, hope it helps :) - Itay Ben Shmuel
Where do I type in ngrok config check? - KingJoeffrey
you can also just type "ngrok config edit" and it will open with your default editor - Joe
This worked for me. Thanks - Dr4v
3
[+10] [2023-02-10 01:38:03] marcio

Using free plan

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

  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!

[1] https://stackoverflow.com/a/38645042/12453339

when you say "in CMD do" what is CMD? Do I open a terminal on my mac? - KingJoeffrey
The original quest was about Windows environment. I think, in Mac, you should use the Terminal! @KingJoeffrey - marcio
When I try this, I get "Your account may not run more than 3 tunnels over a single ngrok agent session." This is immediately after rebooting the machine. It also gives me a list of 3 sessions with names like "tn_2XCpA****************DD7ZBD". How can I find out what they are? - DrGriff
(2) Ah, found the answer here: stackoverflow.com/questions/74512559/…. You have to add bind_tls: true - DrGriff
4
[+7] [2021-09-07 16:16:40] Pedro Dionisio

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.


(2) don't forget to run "ngrok start --all" after those changes - Pedro Dionisio
(1) This doesn't start two ports under one subdomain, it creates two new temporary subdomains. - mmitchell
yes it's true and it works for all your needs :) - Pedro Dionisio
(3) No, it does not. I actually contacted ngrok support over this, and it is not possible to start two ports under the same subdomain. Your config file will start two tunnels under two DIFFERENT subdomains. - mmitchell
5
[+4] [2022-04-14 00:39:03] Viraths

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


NOTE: ERROR: Only paid plans may bind custom subdomains (but one can remove the subdomain) - srghma
Unfortunately, this required purchasing each domain (frontend-my-domain and backend-my-domain), which is what I was trying to avoid. - oalbrecht
6
[+4] [2023-03-10 11:34:48] Stefan Popovski

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

I got this error Tunnel 'all' is not defined in the config files. - famfamfam
7
[+3] [2020-07-28 17:21:08] Josh Osborne

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;
    }
}

down-voting because this answer is duplicate of the solution provided by @King Friday - diramazioni
8
[+1] [2024-09-13 22:23:30] Qui-Gon Jinn

You would need to have a paid account to expose more than one port from the same machine using NGROK.

Edit Ngrok configuration file ngrok.yml

Use this syntax:

version: 3
agent:
  authtoken: 4nq9771bPxe8ctg7LKr_2ClH7Y15Zqe4bWLWF9p
endpoints:
  - name: foo
    description: foo123
    metadata: foo123
    url: foo.ngrok.io
    upstream:
      url: 8080
      protocol: http1
  - name: bar
    url: bar.ngrok.io
    upstream:
      url: 8080

Then run this command to check for errors:

ngrok config check

and this command to start all tunnels or endpoints that are defined in your Ngrok configuration file

ngrok start --all   

9
[0] [2025-02-20 05:00:19] Jay Patel
  1. The following code snippet works with the latest Ngrok version 3.
  2. Please change your ngrok yaml file.
  3. File Location(For Window System): C:\Users\username\AppData\Local\ngrok
  4. You can generate 3 ngrok tunnel in the same machine
version: "3"
tunnels:
    first:
        addr: 4000
        proto: http
    second:
        addr: 5000
        proto: http
agent:
    authtoken: your_private_token

10