Ngrok 在同一域中配置多个端口

是否可以在同一域中打开 恩格罗克中的多个端口?

比如:

转发 http://example.ngrok.com:50001-> 127.0.0.1:50001

转发 http://example.ngrok.com:50002-> 127.0.0.1:50002

我在 windows 中工作,这对于使用 IIS Express调试是很有用的

92074 次浏览

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:

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.

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

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

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.

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