大网数据广告活动
服务器运维问答

I can't avoid 504 gateway time-out

· 52CE 我爱测

In order to avoid the nginx 504 gateway time-out, I've tried to do this in the beginning of my PHP code: set_time_limit(0); Also, I've tried to raise the time settings at /etc/php/7.0/fpm/php.ini : max_execution_time = 3600 max_i…

问题原文

原帖链接 · cawecoy · CC BY-SA 3.0

In order to avoid the nginx 504 gateway time-out, I've tried to do this in the beginning of my PHP code:

set_time_limit(0);

Also, I've tried to raise the time settings at /etc/php/7.0/fpm/php.ini:

max_execution_time = 3600
max_input_time = 3600

And at /etc/php/7.0/fpm/pool.d/www.conf:

request_terminate_timeout=3600

After restarting nginx, I am still getting 504 gateway time-out before 3600 seconds on requests.

What more should I do to avoid it definitely?

已采纳解答

原帖链接 · Jens Bradler · CC BY-SA 3.0

Nginx and Apache with the performant MPM-Event worker are using a dedicated daemon (service) to run PHP. You have already configured this daemon in the right way. But now you have to configure the web server to wait for such a long time. The magic setting can be done with the setting fastcgi_read_timeout.

Configure Nginx to wait some time ...

location ~ \.php$ {

  # allow logging
  access_log              /var/log/nginx/access.log vhosts;

  # include defaults
  include                 fastcgi_params;

  # define connection to php-fpm
  fastcgi_keep_conn       on;
  fastcgi_pass            unix:/var/run/php5-fpm.sock;
  fastcgi_index           index.php;

  # php script name
  fastcgi_param           SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  fastcgi_param           SCRIPT_NAME      $fastcgi_script_name;

  # set buffers
  fastcgi_buffer_size             128k;
  fastcgi_buffers                 256 16k;
  fastcgi_busy_buffers_size       256k;
  fastcgi_temp_file_write_size    256k;

  # allow web-server to wait for a long time before giving up
  fastcgi_read_timeout            3600s;

}

I cannot recommend to activate this setting for a public accessible web server.

If there is a task which needs some time - you should do this task via CLI (command line interface) instead. A good example could be the generation of a Google sitemap.xml of a huge website.

Example:

Execute the script via command line ...

cd /var/www/mysite/ ; php generateSitemap.php

Define a cronjob to execute the script daily via command line ...

0 4 * * *    cd /var/www/mysite/ ; php generateSitemap.php

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。