简体   繁体   中英

Restricting Multiple Node.js Apps to A Specific IP Address On Machine with Multiple IPv4 Addresses

I will be setting up an Ubuntu 19.10 machine that has one-two wired network interface with multiple IP addresses assigned to it (eg. 10.0.0.1 and 10.0.0.2).

How can we run 2 identical Node.js apps such that the first uses 10.0.0.1 and the second uses 10.0.0.2?

In other words, when the first app (using 10.0.0.1) queries a remote API service, the request is seen to originate from 10.0.0.1. The first app also listens to the API responses sent back to 10.0.0.1. Similarly, the second app is restricted to the IP address 10.0.0.2 only.

The API that your application uses to initiate the request probably provides some way of specifying the local address that the request should be associated with. Arrange for one instance of your app to specify 10.0.0.1 as the origin of all of its requests, and have the other instance specify 10.0.0.2.

For example, if you were building the API connection at a low level using a TCP socket then you would specify the desired origin address as a localaddr option to the socket.connect call. However, it's more likely that you're using a higher-level API like the http or https module, where you would specify the desired origin address as a localaddr option to the http.request or https.request call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM