Reverse Proxy to Multiple Endpoints using NGINX

If you are a NodeJS developer, you know that you can easily create multiple Nodes and each of them can have their own ports to connect to.

For example, you might want to display the FrontEnd of the app via http://localhost:8080 and the API via http://localhost:3000

Now, in order to use a single domain to access this, I can use the location block within NGINX configuration and let NGINX forward the request to multiple endpoints depending on the conditions.

Here are the sample server blocks:

server {
    location / {
        proxy_pass http://localhost:8080;
    }

    location /api {
        rewrite ^/api(.*) /$1 break;
        proxy_pass http://localhost:3000;
    }
}

Here the first location block directly forwards the request to http://localhost:8080.
However, for us, the requirement was to make use of API calls as well.

So here the requests like https://www.mydomain.com/apifind/x would simply get forwarded to
http://localhost/find/x. This means, now if I can use the same domain and route the requests to different servers depending on the needs.

 

Happy Coding!

 

Anubhav Ranjan

Anubhav Ranjan

Technical Advisor at Microsoft

Before becoming a Microsoft FTE, Anubhav was a Microsoft MVP, awarded twice consecutively for standing out in the community.

Anubhav is a Program Freak who loves coding and currently works as a Technical Advisor at Microsoft. He is very much passionate about Coding and loves developing Mobile Apps. At present, he helps C# Developers in taking their code across platforms. Also, pretty much interested in the latest ongoing trend which is Bots and AI. Currently working on Microsoft Bot Framework as well.

He is an active member of the BDotnet(Bangalore .NET) User Group and Speaker/Member of XHackers - A Community for Cross-Platform App Development.
Anubhav Ranjan

Leave a Reply