Nginx – How to set memory limit and execution timeout for a specific IP address using php-fpm and nginx

nginxPHPphp-fpm

I need to set a different timeout and execution time for a third party server that needs to import the products and prices on my database.

Right now, they are telling me the php script is timing out. This is due to my configuration on php.ini. I'd like to be able to keep that configuration for everyone as I think that's the right configuration. But I also need them to be able to execute the script.

I guess there must be a way to do this either on nginx or php-fpm configurations. Anyone knows how?

Best Answer

You can use the second php-fpm backend and depending on source IP pass the request to new one. These are some examples.

  1. The php-fpm config wikth required memory limit and execution timeout:
[import]
listen = 127.0.0.1:9001 # use new TCP port/socket
...
php_admin_value[memory_limit] = xxM
php_admin_value[max_execution_time] = xx
  1. By using ngx_http_geo_module and if directive pass to second backend:
geo $import {
 default                0;
 64.95.xx.xx            1;
 80.84.xx.xx            1;
}

server {
  listen 80;
  server_name example.com;
  ...
  location ~ \.php$ {
    include       fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    if ($import) {
            fastcgi_pass   127.0.0.1:9001;
            break;
    }
    fastcgi_pass   127.0.0.1:9000;      #default php-fpm backend
  }
  ...
}