How to setup two node apps and work with nginx?
I have two node apps - one is frontend SPA to be run on port 3333 on node server and another is backend service app running on port 8181. This service should not be publicly accessible as it is to be used by only by the frontend app. The way I understand it is that I have to configure reverse proxy for nginx to proxy all traffic to/from port 80 to port 3333. Another option is to directly deploy frontend app under /var/wwww and let nginx directly serve this app instead of running under node server. What are the pros and cons of these choices? Will performance be better if served directly by nginx?
How to do reverse proxy setup in above scenario and how to prevent public to directly access backend app on port 8181? As as far I know the fronend app will make calls to localhost:8181 to access the backend on same linode?
1 Reply
It seems like what you want to do here is have both the 3333 and 8181 services listening on localhost, and then just use nginx ProxyPass
to proxy connections from port 80/443 to 3333. This is basically what you've already said, I think..but just to be clear that's what I'm talking about doing.
How to do reverse proxy setup in above scenario and how to prevent public to directly access backend app on port 8181? As as far I know the fronend app will make calls to localhost:8181 to access the backend on same linode?
Putting both applications on localhost solves that problem. In terms of whether or not the frontend is connecting to the backend via localhost, you could use a simple tool like ss -plantu
to check your existing connections - or just, you know, look at how it's configured to connect to the backend to begin with.
Once you have both applications listening / connecting to each other via localhost, you just need to do a simple nginx proxy config - they have pretty good docs for that:
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
You're probably going to end up with something that looks sort of like the examples here: