node.jsexpressdockernginx

Express app inside docker. Restricting access to host localhost only


I am trying to restrict access to my express/nodejs app so that it can only be accessed from it's domain url. Currently if I go to http://ip-address-of-server:3000, the app gets served directly, bypassing nginx.

I have tried adding 'localhost' in the app.listen --

app.listen(4000, 'localhost' , () => console.log('Server running'));

but this ends up making the app completely inaccessible. Even through nginx.

The app is running inside a docker container. And nginx is running on the host. I think this might be causing it but don't know how to fix this.


Solution

  • You can use Docker host mode networking for your app since you mentioned that "The app is running inside a docker container. And nginx is running on the host.".

    Ref - https://docs.docker.com/network/host/

    This way, it will be reachable via nginx on the same network. Your "localhost" settings will start working as usual after launching the container in docker host mode networking.