如何在 nginx 配置中为一台主机设置 php ini 设置

How to set php ini settings in nginx config for just one host

可以像这样在 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_VALUEerror_reporting=E_ALL 用于其中一个他们,其他人的空值)。因为 fastcgi_param 如果指定则通过 PHP_VALUE,如果不指定则不通过。一段时间后,所有工作人员都将拥有 PHP_VALUE=error_reporting=E_ALL,除非您在其他主机中明确设置 PHP_VALUE

此外,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

  5. 中将pm改为staticpm.max_children改为1
  6. cat /srv/www/s1/index.php:

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

  8. curl s2 && curl s1 && curl s2

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