If I request data from localhost vs web-address, does the data go through the network
Solution 1:
It depends a bit on the OS and network configuration but there is generally no significant speed difference in communicating with an external IP-address of a server or the localhost / loopback IP-address, as usually the operating system and network stack is aware that the external IP-address is also “this machine” and packets don’t go out on the wire but remain in-memory.
But the additional abstraction layer of nginx in the path between your application and database will add latency compared to having those two communicate directly.
The fastest way to connect your application to your database (when they are both running on the same host) is via a Unix socket (for a database running on Linux or a similar Unix OS ) or a similar direct connection. Such a connection takes away the need to encapsulate the requests and responses in network packets.