Nginx – Making php use specific network interface / IP

networkingnginxPHPphp-fpm

TL;DR: Is there a runtime or php.ini directive for IP/network interface?

Setup: Debian, NginX, php-fpm

The machine has multiple IPs configured in the network interfaces. Each virtual host in NginX is listening to a specific IP. If there is any networking related action performed by PHP, for example a cURL call, the default IP is used every time. If I set CURLOPT_INTERFACE manually in a script, the correct ip is being used, but I need to do this automatically somehow via NginX vhost and php-fpm handshake.

So, site1.com get's called, somehow it needs to pass the ip that that site is listening on to the php-fpm and php-fpm needs to set it as the network interface to use for any Internet connections php will make during execution, be it cURL, get_file_contents(), or any other function.

Can this be done?

Thanks

Best Answer

To answer your TLDR, no that can't be done in PHP itself. There is no global setting that can be changed for all outbound traffic like file_get_contents/etc. As you stated you can customize the interface for curl and several other packages/ components, but there's nothing to globally set it.

Mark Strosberg's answer should work for curl!