Dividing express routes among Node JS Clusters Dividing express routes among Node JS Clusters nginx nginx

Dividing express routes among Node JS Clusters


My solution was to run multiple express apps listened on different ports, and set a Nginx server at front to proxy requests

Say you have three express apps, each one would handle a specific type of routers, and listen on separate port (8081, 8082, 8083), and of course, they should run in cluster mode:

//API app used to handle /api routingapiApp.listen(8081);//Admin app used to handle /admin routingadminApp.listen(8082);//Blog app used to handle /blog routingblogApp.listen(8083);

And config the Nginx server to proxy the requests:

server {    # let nginx server running on a public port     listen       80;    location /api {       proxy_pass http://127.0.0.1:8081    }    location /admin {       proxy_pass http://127.0.0.1:8082    }    location /blog {       proxy_pass http://127.0.0.1:8083    }}

proxy_pass simply tells nginx to forward requests to /api to the server listening on 8081. You can check the full document here