如何从nginx设置一个主机的php ini设置

我们可以在nginx.conf设置error_reporting ,如下所示:

 fastcgi_param PHP_VALUE error_reporting=E_ALL; 

但是如果我在一个服务器块中这样做,它会影响所有其他的呢? 我应该同时更改所有服务器块中的php设置吗?

您可以设置每个服务器的PHP_VALUE ,这只会影响该服务器。 如果所有使用PHP的服务器都需要相同的PHP_VALUE ,请使用包含文件。

例如(debian),创建/etc/nginx/conf.d/php_settings.cnf

 fastcgi_param PHP_VALUE "upload_max_filesize=5M;\n error_reporting=E_ALL;"; 

然后将此文件包含到您需要的任何服务器或位置配置中:

 server { ... location ~ \.php$ { ... include /etc/nginx/conf.d/php_settings.cnf; } ... } 

如果服务器上的每台主机都运行在自己的PHP-FPM池中,那么将fastcgi_param PHP_VALUE ...添加到一个nginx主机不会影响其他主机。

另一方面,如果所有nginx主机都使用一个PHP-FPM池,则应该为每个主机指定PHP_VALUE (其中一个是error_reporting=E_ALL ,其他的为空值)。 由于fastcgi_param指定了传递PHP_VALUE如果不指定则不传递。 一段时间PHP_VALUE=error_reporting=E_ALL ,除非您在其他主机中明确设置了PHP_VALUE ,否则所有工作人员都将拥有PHP_VALUE=error_reporting=E_ALL

此外, fastcgi_param PHP_VALUE ...声明相互覆盖(最后一个生效)。

重现步骤:

  1. apt install nginx php5-fpm

  2. /etc/nginx/sites-enabled/hosts.conf

     server { server_name s1; root /srv/www/s1; location = / { include fastcgi.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_param PHP_VALUE error_reporting=E_ERROR; } } server { server_name s2; root /srv/www/s1; location = / { include fastcgi.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; } } 
  3. s1s2添加到/etc/hosts

  4. /etc/php5/fpm/pool.d/www.conf pm.max_children pm更改为staticpm.max_children1

  5. cat /srv/www/s1/index.php

     <?php var_dump(error_reporting()); 
  6. systemctl restart php5-fpm && systemctl restart nginx

  7. curl s2 && curl s1 && curl s2

     int(22527) int(1) int(1)