0

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

2 Answers 2

1

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!

0

In your Nginx configuration, you can add this, setting the number to the desired IP.

 fastcgi_param CURLOPT_INTERFACE 1.2.3.4; 

This will populate the $_SERVER variable inside the PHP script.

In your PHP script, set CURLOPT_INTERFACE to $_SERVER['CURLOPT_INTERFACE'];

I've not tested this, but I think it should work.

1
  • fastcgi_param PHP_VALUE sets the php runtime directives, but there does not seem to be an option in php configuration to bind to specific ip/interface. or is there? Commented Jan 21, 2016 at 18:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.