Go back

Using nginx as a load balancer for my ngrok exposed local services

2025-08-24 23:18 - 2025-08-24 23:46 (88c27ea), 2 min read
#web

Just a small post sharing something I learned last week. This is the kind of post that could be a tweet, but instead, I want to put it here, on my blog. This was useful for me, I know it can be again in the future, hope it will be useful for you too at some point.

Last week, I had to do some tests given a specific scenario: having a load balancer which should redirect to the same application running in 3 different ports in parallel. After thinking a bit about how could I have the best setup to test it, I found a super easy way to handle this: nginx.

I found this documentation here, which explains to me exactly how I could achieve this: specifially using a keyword called upstream. Using it with the proxy_pass, will let me to put my reverse proxy to target this set of servers, where I can select how I want to route of each of the exposed servers there (by default, it’s routed as round robin).

The nginx configuration would be like this:

worker_threads auto;
 
http {
    upstream backend {
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }
 
    server {
        listen 8888;
 
        location / {
            proxy_pass http://backend;
        }
    }
}

On the documentation I shared before, it also shows some other options and other use cases you can achieve using this upstream keyword.

After that, I just need to expose my ngrok service to expose its reverse proxy to listen into the port 8888, as I said in my nginx.conf:

ngrok http --url=example.ngrok-free.app 8888

Now, all the requests that comes in https://example.ngrok-free.app will be redirect automatically to my local reverse proxy which will be automatically handled by its load balancer.