是否可以在每个位置的基础上延长 nginx 中的 504 超时

cod*_*boy 6 nginx php-fpm ubuntu-14.04

是否可以在位置块中设置超时指令以防止 nginx 从长时间运行的 PHP 脚本 (PHP-FPM) 返回 504?

server
{
  listen 80;
  server_name ubuntu-vm.test-api;
  root /home/me/Sites/path/to/site/; 
  index index.php;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {


    try_files $uri =404;

    # Fix for server variables that behave differently under nginx/php-fpm than typically expected
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    # Include the standard fastcgi_params file included with nginx
    include fastcgi_params;
    fastcgi_param  PATH_INFO        $fastcgi_path_info;
    fastcgi_index index.php;

    # Override the SCRIPT_FILENAME variable set by fastcgi_params
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
    fastcgi_pass unix:/var/run/php5-fpm.sock;

    }

    location /someurlpath {

    try_files $uri $uri/ /index.php?$query_string;

    # Fix for server variables that behave differently under nginx/php-fpm than typically expected
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    # Include the standard fastcgi_params file included with nginx
    include fastcgi_params;
    fastcgi_param  PATH_INFO        $fastcgi_path_info;
    fastcgi_index index.php;

    # Override the SCRIPT_FILENAME variable set by fastcgi_params
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
    fastcgi_pass unix:/var/run/php5-fpm.sock;

    fastcgi_read_timeout 100000s;
    }

    error_log /var/log/nginx/my_api_error.log;
    access_log /var/log/nginx/my_api_access.log;
}
Run Code Online (Sandbox Code Playgroud)

这在向 发出请求时无效example.com/someurlpath。超时发生在大约 60 秒后。PHP 配置为允许脚本运行直到完成(set_time_limit(0))

如果我fastcgi_read_timeout在主~ /.php {}块中设置,这可以解决问题。

我不想为所有脚本设置全局超时。

moe*_*eye 5

首先,看一下嵌套位置。不考虑第二个位置块的原因是当 nginx 匹配一个位置时,它会停止。所以,http://ubuntu-vm.test-api/someurlpath如果对应的文件夹中有index.php,则只匹配location ~ \.php$

我偶然发现了这篇有趣的博客文章

总而言之,您需要:

  1. 增加max_execution_timephp.ini 中的配置变量。
  2. 增加request_terminate_timeoutphp-fpm的配置变量。
  3. fastcgi_read_timeout在 nginx 配置文件中的所需位置 进行设置。

问题是你不能告诉 php-fpm 仅针对该一个位置使用不同的配置文件。

但是,您可以在 nginx 配置中设置 php.ini 配置变量,如下所示:

fastcgi_param PHP_VALUE "max_execution_time=1000";
Run Code Online (Sandbox Code Playgroud)